2. ITERATORS: CONDITIONAL
LOOPING
• “while” allows us to loop through code while a set condition is
true
x = 1
while x < 10
puts x.to_s + “ iteration”
x += 1
end
3. TIMES
5.times { puts “hello” }
5.times { |num| puts “hi”+num.to_s }
99.times do |beer_num|
puts "#{beer_num} bottles of beer”
end
99.times do
puts "some bottles of beer”
end
4. CREATING A NEW ARRAY
x = [1, 2, 3, 4]
=> [1, 2, 3, 4]
x = %w(1 2 3 4)
=> [“1”, “2”, “3”, “4”]
chef = Array.new(3,
“bork”)
=> [“bork”, “bork”, bork”]
5 is an object that is an instance of the integer class\ntimes is a method of the 5 object\ntimes is a method on an object that is an instance of integer\n
A lot of the time you will be using an array when you iterate over something\nAn array is just a list of items. \nEvery spot in the list acts like a variable and you can make each spot point to a different object\n\nW means words\n\nArray is a class, needs to start with capital letter\n
IRB\nif you go off the array it will be nil\n
\n
join is cool because it makes a string for you\n\nshovel operator\n\nmultidimensional array\n
Does anyone know what a hash is? \nassociative array \ncollection of key-value pairs\nkeys can be numbers or strings \n\nDifference from an Array\n
merge takes the value from the second hash\nmerge! changes h1\n
you would think that delete should need a bang to change the hash, but delete doesn&#x2019;t exist with a bang\ndelete returns the value\n