Tuesday, June 3, 2014

Arrays

from rubylearning,com
"An Array is just a list of items in order (like mangoes, apples, and oranges). Every slot in the list acts like a variable: you can see what object a particular slot points to, and you can make it point to a different object. You can make an array by using square brackets In Ruby, the first value in an array has index 0. The size and lengthmethods return the number of elements in an array. The last element of the array is at index size-1. Negative index values count from the end of the array, so the last element of an array can also be accessed with an index of -1. If you attempt to read an element beyond the end of an array (with an index >= size) or before the beginning of an array (with an index < -size), Ruby simply returns nil and does not throw an exception. Ruby's arrays are mutable - arrays are dynamically resizable; you can append elements to them and they grow as needed."

locations.each do |loc|
  puts 'I love ' + loc + '!'
  puts "Don't you?"
end

Great examples
http://zetcode.com/lang/rubytutorial/arrays/

method shuffle

puts "#{planets.shuffle}"


http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-shuffle
a.shuffle(random: Random.new(1))  #=> [1, 3, 2]


The Basics of Ruby Arrays
http://blog.teamtreehouse.com/ruby-arrays

SHUFFLE
http://stackoverflow.com/questions/1816378/how-to-randomly-sort-scramble-an-array-in-ruby
[1,2,3,4].shuffle => [2, 1, 3, 4]
[1,2,3,4].shuffle => [1, 3, 2, 4]

http://stackoverflow.com/questions/15974992/reproduce-random-array-sort
> arr = [1, 2, 3, 4] => [1, 2, 3, 4]
>> r = Random.new(17) => #<Random:0x000000017be4d0>
>> arr.shuffle(random: r) => [3, 1, 4, 2]
>> arr.shuffle(random: r) => [1, 3, 2, 4]
>> arr.shuffle(random: r) => [4, 3, 2, 1]
>> r = Random.new(17) => #<Random:0x00000001c60da8>
>> arr.shuffle(random: r) => [3, 1, 4, 2]
>> arr.shuffle(random: r) => [1, 3, 2, 4]
>> arr.shuffle(random: r) => [4, 3, 2, 1]
>> etc.
?>

http://www.rubymotion.com/developer-center/api/Array.html

http://tamlearnstocodewithoutsusanne.tumblr.com/

No comments:

Post a Comment