# Passing a method reference and then calling it

**URL:** <https://www.ruby-forum.com/t/passing-a-method-reference-and-then-calling-it/171128>\
**Category:** Ruby\
**Created:** [June 26, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/passing-a-method-reference-and-then-calling-it/171128 "2009-06-26T20:04:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 26, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/passing-a-method-reference-and-then-calling-it/171128/1 "2009-06-26T20:04:14Z")

</div>

How do you pass a method reference and call it on a particular object?  
I know I can pass it as a string and call eval, but I’m trying to  
avoid that performance hit.

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 26, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/passing-a-method-reference-and-then-calling-it/171128/2 "2009-06-26T20:21:48Z")

</div>

On Fri, Jun 26, 2009 at 2:03 PM, Martin H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do you pass a method reference and call it on a particular object? I  
> know I can pass it as a string and call eval, but I’m trying to avoid that  
> performance hit.

You could use a proc/lambda, and pass it just like any other object:  
foo = lambda do  
puts “word.”  
end

def test(bar)  
bar.call  
end

test(foo)

Alex

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 26, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/passing-a-method-reference-and-then-calling-it/171128/3 "2009-06-26T20:26:29Z")

</div>

Martin H. wrote:

> How do you pass a method reference and call it on a particular object? I  
> know I can pass it as a string and call eval, but I’m trying to avoid  
> that performance hit.

obj = “something”  
method = :reverse  
obj.send method # faster than eval

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 26, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/passing-a-method-reference-and-then-calling-it/171128/4 "2009-06-26T20:27:39Z")

</div>

That works, but what if I want to store the method reference for later  
use?

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 26, 2009, 8:36pm UTC](https://www.ruby-forum.com/t/passing-a-method-reference-and-then-calling-it/171128/5 "2009-06-26T20:36:26Z")

</div>

That is exactly what I was look for! Thank you very much.
