two arguments in a block?

K

Kev Jackson

Hi,

I want to pass an array into a block like so

v2 = []
object.some_method_that_takes_a_block do | v1, v2|
v2.push v1.action
end

causes a undefined method push for nil

the problem is that v2 gets assigned as nil as it gets passed through,
is there any way around this?

I don't really want to open the original class and try to add a new
method signature (I hope that's not how to get it to accept to variables)

Kev
 
P

Peter C. Verhage

Hi,

Have you tried:

v2 = []
object.some_method_that_takes_a_block do | v1 |
v2.push v1.action
end

Regards,

Peter
 
D

Daniel Wislocki

You're not actually "passing" v2 anywhere. The |v1, v2| creates two
variables, v1 and v2, local to that block. It's the method itself that
supplies the values of v1 and v2. Are you sure what you're trying to
do is correct?
 
K

Kev Jackson

Peter said:
Hi,

Have you tried:

v2 = []
object.some_method_that_takes_a_block do | v1 |
v2.push v1.action
end

Regards,

Peter
DOH!

Sorry all, that was really #*&(^)@ dumb of me

Thanks for the clue

Kev
 
D

David A. Black

Hi --

You're not actually "passing" v2 anywhere. The |v1, v2| creates two
variables, v1 and v2, local to that block. It's the method itself that
supplies the values of v1 and v2. Are you sure what you're trying to
do is correct?

It's a little different than that: at the time of |v1,v2| v2 already
exists, since it was created before the block. So the overall effect
is like:

v2 = []
v1, v2 = some_scalar_value

which reassigns nil to v2.


David

Hi,

I want to pass an array into a block like so

v2 = []
object.some_method_that_takes_a_block do | v1, v2|
v2.push v1.action
end

causes a undefined method push for nil

the problem is that v2 gets assigned as nil as it gets passed through,
is there any way around this?

I don't really want to open the original class and try to add a new
method signature (I hope that's not how to get it to accept to variables)

Kev
 

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

No members online now.

Forum statistics

Threads
474,186
Messages
2,570,998
Members
47,587
Latest member
JohnetteTa

Latest Threads

Top