send() with a block?

7

7stud --

Thanks for all the responses. This may be true:

--
The actual method "send" can take a block, and it passes it to the
method being invoked. If it didn't, it would be impossible to pass a
block to a method when invoking it using send.
--

...but why should a beginner have to figure that out? You shouldn't
have to be a meta-programming guru to figure out the docs. Wouldn't it
be better if beginners posted: "I really think the docs are great."
rather than "beginner: the docs are confusing; expert-response: they are
clear if you are smart."


Why doesn't this work:

def my_meth(*args)
yield
p args
end

my_proc = Proc.new {puts 'hello'}
my_meth(1, 2, 3, &my_proc ) #=>'hello' [1, 2, 3]

obj = Object.new
m = obj.method(my_meth)
m.call([10, 20, 30], &my_proc) #LocalJumpError at yield line
 
G

Gary Wright

Why doesn't this work:

def my_meth(*args)
yield
p args
end

my_proc = Proc.new {puts 'hello'}
my_meth(1, 2, 3, &my_proc ) #=>'hello' [1, 2, 3]

obj = Object.new
m = obj.method(my_meth)
m.call([10, 20, 30], &my_proc) #LocalJumpError at yield line

Your comment about LocalJumpError should be one line above and it
occurs because you are calling my_meth without a block and are
attempting to yield to the non-existent block. You don't want
to invoke my_meth there, you want to *name* it:
m = obj.method('my_meth')


I think you are adding a confusing difference when you try to invoke
my_method on the last line by packaging up three arguments in an array.
If you want to follow the pattern you used when you called my_meth
directly you should be doing:
m.call(10, 20, 30, &my_proc)


Gary Wright
 
7

7stud --

Gary Wright wrote in post #987429:
obj = Object.new
m = obj.method(my_meth)
m.call([10, 20, 30], &my_proc) #LocalJumpError at yield line

Your comment about LocalJumpError should be one line above and it
occurs because you are calling my_meth without a block and are
attempting to yield to the non-existent block. You don't want
to invoke my_meth there, you want to *name* it:
m = obj.method('my_meth')

Ah, a typo. Thanks.
 

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
473,777
Messages
2,569,604
Members
45,220
Latest member
MathewSant

Latest Threads

Top