# How to use a proc like a method

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-proc-like-a-method/50673>\
**Category:** Ruby\
**Created:** [December 19, 2005, 9:59pm UTC](https://www.ruby-forum.com/t/how-to-use-a-proc-like-a-method/50673 "2005-12-19T21:59:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hochherz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/85f322/32.png) [@hochherz](https://www.ruby-forum.com/u/hochherz)\
**Post date:** [December 19, 2005, 9:59pm UTC](https://www.ruby-forum.com/t/how-to-use-a-proc-like-a-method/50673/1 "2005-12-19T21:59:56Z")

</div>

is there a way to use a proc like a method?

like:

class A  
def initialize  
@av=10;  
end  
def do(mproc)  
mproc.call();  
end  
def rt()  
@av;  
end  
end

a=proc{@av=111} #something different  
b=A.new  
b.do(a)  
b.rt #return -\> 111

is it possible by makeing subclass of proc to do this?

---

<div class="post-metadata">

**Author:** ![hochherz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/85f322/32.png) [@hochherz](https://www.ruby-forum.com/u/hochherz)\
**Post date:** [December 19, 2005, 10:09pm UTC](https://www.ruby-forum.com/t/how-to-use-a-proc-like-a-method/50673/2 "2005-12-19T22:09:08Z")

</div>

# ------------ 8\< ------------

class A  
def initialize  
@av=10;  
end  
def do(mproc)  
#### changed this line ####  
instance\_eval &mproc  
end  
def rt()  
@av;  
end  
end

a=proc{@av=111} #something different  
b=A.new  
b.do(a)  
b.rt #return -\> 111

# ------------ 8\< ------------

Jacob F.

---

<div class="post-metadata">

**Author:** ![hochherz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/85f322/32.png) [@hochherz](https://www.ruby-forum.com/u/hochherz)\
**Post date:** [December 19, 2005, 10:21pm UTC](https://www.ruby-forum.com/t/how-to-use-a-proc-like-a-method/50673/3 "2005-12-19T22:21:27Z")

</div>

Now that I answered the simple questions, I’d suggest some style  
changes/fixes:

# ------------ 8\< ------------

class A  
def initialize  
@av = 10 ### [1], [2]  
end  
def do(&mproc) ### [3]  
instance\_eval &mproc  
end  
def rt ### [4]  
@av  
end  
end

a = proc{ @av = 111 } # something different  
b = A.new  
b.do(&a) ### [5]  
b.do { @av = 111 } ### [6]  
b.rt # return -\> 111

# ------------ 8\< ------------

[1] Ruby doesn’t need a semicolon. The newline is the statement  
separator. A semicolon can be used to separate multiple statements on  
the same line, however.

[2] Be liberal with your whitespace. This is simply style, but I think  
you’ll find most rubyists share this preference.

[3] You can make a method accept a block parameter (such as Array#each  
does) by prepending the last parameter in the parameter list with an  
ampersand (&).

[4] If you are defining a method with no parameters, it’s acceptable  
to leave off the empty parentheses. This is also just style and may be  
debated.

[5] You can change a Proc object into a block with the & prefix. (In  
fact, you can turn any object into a block with & and a properly  
defined to\_proc method. Whether doing so is good practice is  
debatable.)

[6] The obvious alternative to creating an explicit proc then  
converting that proc to a block.

Jacob F.

---

<div class="post-metadata">

**Author:** ![hochherz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/85f322/32.png) [@hochherz](https://www.ruby-forum.com/u/hochherz)\
**Post date:** [December 19, 2005, 11:12pm UTC](https://www.ruby-forum.com/t/how-to-use-a-proc-like-a-method/50673/4 "2005-12-19T23:12:57Z")

</div>

On Dec 19, 2005, at 4:19 PM, Jacob F. wrote:

> [5] You can change a Proc object into a block with the & prefix. (In  
> fact, you can turn any object into a block with & and a properly  
> defined to\_proc method. Whether doing so is good practice is  
> debatable.)

This doesn’t seem quite accurate to me. The expression generated  
by the use of ‘&’ in a method call is an instance of Proc. If the  
expression prefixed by ‘&’ is already an instance of Proc then  
no conversion of any type occurs, the object is passed ‘as is’ to  
the method.

If might be more accurate to say:

Instead of using a literal block in a method call, you can arrange  
for any object to be passed as the ‘block argument’ to the method  
with the ‘&’ prefix operator:

```
method(arg1, arg2, arg3, &block_arg)

```

If ‘block\_arg’ is not an instance of Proc, then block\_arg.to\_proc  
is called and the result, which must be an instance of Proc, is  
passed to the method instead.

---

<div class="post-metadata">

**Author:** ![hochherz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/85f322/32.png) [@hochherz](https://www.ruby-forum.com/u/hochherz)\
**Post date:** [December 20, 2005, 12:01am UTC](https://www.ruby-forum.com/t/how-to-use-a-proc-like-a-method/50673/5 "2005-12-20T00:01:51Z")

</div>

On 12/19/05, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> no conversion of any type occurs, the object is passed ‘as is’ to  
> If ‘block\_arg’ is not an instance of Proc, then block\_arg.to\_proc  
> is called and the result, which must be an instance of Proc, is  
> passed to the method instead.

That’s very true and accurate. Thanks for the clarification. My  
original intent was to say “You can [pass] a Proc object into a block  
[argument] with the & prefix.” The type of the object doesn’t change,  
just the passing semantics. And, “In fact, you can turn any object  
into a [Proc with block argument semantics] with & and a properly  
defined to\_proc method.” Careless terminology and lack of  
proofreading, my apologies.

Jacob F.
