# On procs and methods

**URL:** <https://www.ruby-forum.com/t/on-procs-and-methods/185671>\
**Category:** Ruby\
**Created:** [March 24, 2010, 2:20pm UTC](https://www.ruby-forum.com/t/on-procs-and-methods/185671 "2010-03-24T14:20:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [March 24, 2010, 2:20pm UTC](https://www.ruby-forum.com/t/on-procs-and-methods/185671/1 "2010-03-24T14:20:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [March 25, 2010, 4:55am UTC](https://www.ruby-forum.com/t/on-procs-and-methods/185671/2 "2010-03-25T04:55:33Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [March 25, 2010, 10:31am UTC](https://www.ruby-forum.com/t/on-procs-and-methods/185671/3 "2010-03-25T10:31:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [March 25, 2010, 12:05pm UTC](https://www.ruby-forum.com/t/on-procs-and-methods/185671/4 "2010-03-25T12:05:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [March 25, 2010, 11:09am UTC](https://www.ruby-forum.com/t/on-procs-and-methods/185671/5 "2010-03-25T11:09:33Z")

</div>

2010/3/24 David E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
