Saturday, September 20, 2008

More on enumerable

In ruby, I use .each alot to visit every elements in the array.
Did you know there's a .step(x) function that allows you to visit
every x th element in the array?

For example:

r = 0..3
r.each {|l| prints l.to_s + ' ' } # Prints 0 1 2 3

r.step(2) {|l| prints l.to_s + ' '} # Prints 0 2


J

No comments: