/ Published in: Ruby
For all of you actionscripters out there, this example is the Ruby equivalent of writing for(i in array){trace(i);} or for(i = 0; i < array.length; i++){trace(i);}
Expand |
Embed | Plain Text
#A. Create an array names = %w[chris sandy josie billy suzie] #B. Find the length of the array and iterate through it names.length.times do |i| puts i.to_s + " " + names[i] end
Comments
Subscribe to comments
You need to login to post a comment.

With the names array already initialized, a more idiomatic iteration would be:
names.eachwithindex { |name, i| puts "#{i} #{name}" }
That would be:
names.each_with_index { |name, i| puts "#{i} #{name}" }Thanks for the tip ed!