# Is there a way to use a proc like a method?

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-to-use-a-proc-like-a-method/72639>\
**Category:** Ruby\
**Created:** [December 19, 2005, 9:59pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-a-proc-like-a-method/72639 "2005-12-19T21:59:57Z")\
**Posts on this page:** 6\
**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/is-there-a-way-to-use-a-proc-like-a-method/72639/1 "2005-12-19T21:59:57Z")

</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/is-there-a-way-to-use-a-proc-like-a-method/72639/2 "2005-12-19T22:09:06Z")

</div>

hochherz schrieb:

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

class A  
def do(mproc)  
instance\_eval(&mproc)  
end  
end

Regards,  
Pit

---

<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:15pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-a-proc-like-a-method/72639/3 "2005-12-19T22:15:14Z")

</div>

On Mon, 19 Dec 2005 20:50:44 -0000, hochherz  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

Maybe use:

```
 def do(mproc)
   instance_eval &mproc
 end

```

instead of ‘call’

---

<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:00pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-a-proc-like-a-method/72639/4 "2005-12-19T23:00:46Z")

</div>

You could also do something like this:

a=proc{@av=111} #something different  
a.bind(b).call

-Jeff

---

<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, 6:59am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-a-proc-like-a-method/72639/5 "2005-12-20T06:59:23Z")

</div>

BTW, a more common way in the Ruby world to do the sort of thing it  
seems you’re after is this:

class A  
attr\_accessor :av  
def do(mproc)  
mproc.call(self)  
end  
end

a = proc {|obj| obj.av = 111}

I see two advantages of that:

- You don’t break encapsulation, and thus get complete control over your  
class’s interaction with client code.
- The Proc created in the client code gets to refer to its own instance  
variables and methods.

And, yes, I agree with the other comments, too. So, here’s my new  
version:

class A  
attr\_accessor :av  
def initialize  
@av = 10  
end  
def do  
yield self  
end  
end

b=A.new  
b.do {|obj| obj.av = 111}  
b.av #=\> 111  
a = lambda {|obj| obj.av = 112}  
b.do &a  
b.av #=\> 112

Of course, in this simple example, you could just drop the ‘def  
do…end’, and say:

b = A.new  
b.av = 111  
b.av #=\> 111

But I’m guessing that’s not what you’re after.

Devin

---

<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:03pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-a-proc-like-a-method/72639/6 "2005-12-19T23:03:48Z")

</div>

But apparently this doesn’t work in practice, scratch that, hehe.
