Loops
Goals
- Use loops to do operations for every element in an array.
-
Use
putsto print strings to the screen. - Learn the two different syntaxes for creating blocks in Ruby.
Step 1
Type this in irb:puts 'Hello World'Expected result:1.9.3p125 :006 > puts 'Hello World' Hello World => nil 1.9.3p125 :007 >
Step 2
Type this in irb:fruits = ['peach', 'plum', 'pear'] fruits.each { |fruit| puts fruit }
Step 3
Type this in irb:numbers = [109, 10, 1001] numbers.each { |n| puts n * 2 }Type this in irb:ducks = ['huey', 'dewey', 'louie'] ducks.each { |duck| puts "#{duck} quacks!" } ducks.each { |zombie| puts "#{zombie} quacks!" }
Step 4
Type this in irb:total = 256 ** 3 colors = ['red', 'blue', 'green'] colors.each do |color| puts "#{total} colors of paint on the wall..." puts "Take #{color} down, pass it around..." total = total - 1 puts "#{total} colors of paint on the wall!" end
Explanation
Next Step:
Go on to Running Programs From A File
Back to Hashes