Tuesday, November 01, 2005

Working in Ruby

So I'm working in ruby and I'd like to get the following from one list:

lines = %w{a b c d e f}


irb(main):211:0> colors = lines.clone
=> ["a", "b", "c", "d", "e", "f"]
irb(main):212:0> (1..colors.length/2).map {|z| [ colors.shift ] + [ colors.shift ] }
=> [["a", "b"], ["c", "d"], ["e", "f"]]


so either [a,b], [c,d],[e,f] as a list (which I l hate how the above destroys colors inside the loop
or
a,b ; b,c ; c,d ; d,e ; e,f as a list.

I did use the following for the second part:


colors[0..-2].zip(colors[1..-1])


Any hints,tips suggestions would be appreciated.

No comments: