On procs and methods

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.

On Wednesday 24 March 2010 08:20:06 am David E. wrote:

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…

David E. wrote:

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 M. 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.

El jueves 25 de marzo, Albert S. 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.

2010/3/24 David E. [email protected]:

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:[email protected](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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs