Troubles with blocks

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
 
J

Jason Roelofs

[Note: parts of this message were removed to make it a legal post.]

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
Hmm, I can't find a shorter way than what you've got. Why are you trying to
get this kind of information? Maybe there's something earlier in your code
that can be made more Ruby-esque to make this easier for you.

Jason
 
G

Gary Wright

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?

In Ruby 1.8:

require 'enumerator'
%w{a b c}.enum_for:)each_with_index).to_a


In Ruby 1.9:

%w{a b c}.each_with_index.to_a

Gary Wright
 
M

MenTaLguY

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?

Try this (you will need to require 'enumerator'):

['a','b','c'].enum_with_index.to_a

-mental
 
J

Jason Roelofs

[Note: parts of this message were removed to make it a legal post.]

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?

Try this (you will need to require 'enumerator'):

['a','b','c'].enum_with_index.to_a

-mental
Ah, that's where enum_with_index is. I was wondering why it wasn't working
for me.

Jason
 
H

Holden Holden

Gary said:
In Ruby 1.8:

require 'enumerator'
%w{a b c}.enum_for:)each_with_index).to_a

In Ruby 1.9:

%w{a b c}.each_with_index.to_a

Thanks! that's exactly what I was looking for. I had already tried the
last solution but I got a "no block given" error (I'm using Ruby 1.8).
I'm glad to hear that versions 1.9 and higher support this construct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,213
Messages
2,571,109
Members
47,701
Latest member
LeoraRober

Latest Threads

Top