On procs and methods

D

David Espada

Hi.

I have a question about procs and methods. Why is not possible to invoke
them in same form:

* proc: foo[param1, param2]
* method: foo(param1, param2)

Only curious. Thanks for your ideas.
 
D

David Masover

I have a question about procs and methods. Why is not possible to invoke
them in same form:

Because the syntax for calling a method always calls a method. Let me put it
this way:
* proc: foo[param1, param2]
* method: foo(param1, param2)

The only way you can have something called 'foo' is if there's a local
variable, or if there's a method with the name 'foo'. For example, if there
isn't a local variable, your code could be interpreted as:

foo()[param1, param2]

Does that make sense? So right now, this:

foo(param1, param2)

will always call the method 'foo'. With what you're suggesting, it'd have to
check for a local variable first -- and what would happen if the method 'foo'
returns a proc, how would I call that? Would I do:

foo()(param1, param2)

Wouldn't that get confusing?

Basically, procs aren't methods, they're objects. When you use that [] syntax,
you're actually calling the [] method on the foo object. It gets interpreted
like this:

foo.[](param1, param2)

So these mean fundamentally different things.

Now, you could take a proc and turn it into a method with define_method...
 
A

Albert Schlef

David said:
Hi.

I have a question about procs and methods. Why is not possible to invoke
them in same form:

* proc: foo[param1, param2]
* method: foo(param1, param2)

Only curious. Thanks for your ideas.

(The following is only a guess.)

Ruby lets you drop the parenthesis. Now, look at this:

a = bobo

Suppose 'bobo' is a variable that points to a proc. Whould that
statement assign it to 'a', or invoke the proc and assign the result to
'a'? You can't know.

David Masover gave the following example...

foo()(param1, param2)

...and since Ruby lets you drop the parenthesis, you should be able to
write is as:

foo(param1, param2)

...but that's a problem because there's no way to distinguish between
calling foo with arguments to calling foo and then calling the returned
value.

In languages like JavaScript the parenthesis are mandatory so they don't
have similar problems.
 
R

Robert Klemme

2010/3/24 David Espada said:
I have a question about procs and methods. Why is not possible to invoke
them in same form:

* proc: foo[param1, param2]
* method: foo(param1, param2)

Only curious. Thanks for your ideas.

Well, you *can* - in a way:

irb(main):001:0> def foo(*a) printf "method called%p\n", a end
=> nil
irb(main):002:0> foo = lambda {|*a| printf "proc called%p\n", a}
=> #<Proc:0x1015c594@(irb):2 (lambda)>
irb(main):003:0> foo[1,2,3]
proc called[1, 2, 3]
=> nil
irb(main):004:0> method:)foo)[4,5,6]
method called[4, 5, 6]
=> nil

Kind regards

robert
 
D

David Espada

El jueves 25 de marzo, Albert Schlef escribió:
Ruby lets you drop the parenthesis. Now, look at this:

a = bobo

Suppose 'bobo' is a variable that points to a proc. Whould that
statement assign it to 'a', or invoke the proc and assign the result to
'a'? You can't know.

Thank you for all your responses. Now I see better what is the problem.

Greets.
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top