How to iterate through an array in Ruby


/ 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);}


Copy this code and paste it in your HTML
  1. #A. Create an array
  2. names = %w[chris sandy josie billy suzie]
  3.  
  4. #B. Find the length of the array and iterate through it
  5. names.length.times do |i|
  6. puts i.to_s + " " + names[i]
  7. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.