H
Holden Holden
Hi,
I am a Python programmer and I've just started learning Ruby. Until now,
I've enjoyed it, but I am having troubles with some methods that require
blocks.
This is a simple example that (I expect) summarizes my problems - how to
obtain a list of pairs [index, value] (or [value, index], it doesn't
matter) from an array:
Python:
list(enumerate(['a','b','c']))
=> [(0, 'a'), (1, 'b'), (2, 'c')]
Ruby:
t = []; ['a','b','c'].each_with_index { |*x| t << x }; t
=> [["a", 0], ["b", 1], ["c", 2]]
Which is a very ugly solution. How could I do this on Ruby without a
temporal variable?
thanks
I am a Python programmer and I've just started learning Ruby. Until now,
I've enjoyed it, but I am having troubles with some methods that require
blocks.
This is a simple example that (I expect) summarizes my problems - how to
obtain a list of pairs [index, value] (or [value, index], it doesn't
matter) from an array:
Python:
list(enumerate(['a','b','c']))
=> [(0, 'a'), (1, 'b'), (2, 'c')]
Ruby:
t = []; ['a','b','c'].each_with_index { |*x| t << x }; t
=> [["a", 0], ["b", 1], ["c", 2]]
Which is a very ugly solution. How could I do this on Ruby without a
temporal variable?
thanks