# Passing method references in python & ruby

**URL:** <https://rubytalk.org/t/passing-method-references-in-python-ruby/43862>\
**Category:** ruby-talk\
**Created:** [18 January 2008 01:46 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862 "2008-01-18T01:46:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy\_Pardee](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Roy\_Pardee](https://rubytalk.org/u/Roy_Pardee)\
**Post date:** [18 January 2008 01:46 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/1 "2008-01-18T01:46:04Z")

</div>

Hey all,

Kind of an idle, syntax comparison question here...

In python, you can apparently pass method references around very  
easily. So imagine you have these 2 functions defined:

&nbsp;&nbsp;&nbsp;def firstWay(arg1, arg2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 'Tastes great.'

&nbsp;&nbsp;&nbsp;def secondWay(arg1, arg2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 'Less filling.'

Then you can define a method that takes a method name as an argument,  
and calls it just by throwing parens (and any expected arguments of  
course) after it.

&nbsp;&nbsp;&nbsp;def doStuff(whichway, first\_arg, second\_arg)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return whichway(first\_arg, second\_arg)

So calling:

&nbsp;&nbsp;&nbsp;puts doStuff(firstWay, None, None)

Would result in 'Tastes great.'

What's the most graceful way to do this sort of thing in ruby? I  
tried passing in e.g., firstWay.to\_proc, but that got me a complaint  
about not having enough arguments on the call. I can imagine doing,  
e.g.,

&nbsp;&nbsp;&nbsp;first\_pointer = Proc.new do |foo, bar|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return firstWay(foo, bar)  
&nbsp;&nbsp;&nbsp;end

One for each alternate method & passing one of those in. I can also  
imagine having doStuff take a block that would call the desired  
function & yielding out to that. But neither of those are as pretty  
as the python I think. Are those my best options in ruby, or is there  
another way?

Thanks!

-Roy

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [18 January 2008 04:12 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/2 "2008-01-18T04:12:33Z")

</div>

rpardee@gmail.com wrote:

> Hey all,
> 
> Kind of an idle, syntax comparison question here...
> 
> In python, you can apparently pass method references around very  
> easily. So imagine you have these 2 functions defined:
> 
> &nbsp;&nbsp;&nbsp;def firstWay(arg1, arg2)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 'Tastes great.'
> 
> &nbsp;&nbsp;&nbsp;def secondWay(arg1, arg2)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 'Less filling.'
> 
> Then you can define a method that takes a method name as an argument,  
> and calls it just by throwing parens (and any expected arguments of  
> course) after it.
> 
> &nbsp;&nbsp;&nbsp;def doStuff(whichway, first\_arg, second\_arg)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return whichway(first\_arg, second\_arg)
> 
> So calling:
> 
> &nbsp;&nbsp;&nbsp;puts doStuff(firstWay, None, None)
> 
> Would result in 'Tastes great.'
> 
> What's the most graceful way to do this sort of thing in ruby? I  
> tried passing in e.g., firstWay.to\_proc, but that got me a complaint  
> about not having enough arguments on the call. I can imagine doing,  
> e.g.,
> 
> &nbsp;&nbsp;&nbsp;first\_pointer = Proc.new do |foo, bar|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return firstWay(foo, bar)  
> &nbsp;&nbsp;&nbsp;end
> 
> One for each alternate method & passing one of those in. I can also  
> imagine having doStuff take a block that would call the desired  
> function & yielding out to that. But neither of those are as pretty  
> as the python I think. Are those my best options in ruby, or is there  
> another way?
> 
> Thanks!
> 
> -Roy

Here is another way:

irb(main):001:0\> def dostuff(whichway, arg1, arg2)  
irb(main):002:1\> send(whichway, arg1, arg2)  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> def first\_way(arg1, arg2)  
irb(main):005:1\> "tastes great"  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> def second\_way(arg1, arg2)  
irb(main):008:1\> "less filling"  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> dostuff(:first\_way, nil, nil)  
=\> "tastes great"  
irb(main):011:0\> dostuff(:second\_way, nil, nil)  
=\> "less filling"  
irb(main):012:0\> "No! #{dostuff(:first\_way, nil, nil).capitalize}!"  
=\> "No! Tastes great!"

-Justin

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [18 January 2008 15:38 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/3 "2008-01-18T15:38:35Z")

</div>

> \> Hey all,  
> \>  
> \> Kind of an idle, syntax comparison question here...  
> \>  
> \> In python, you can apparently pass method references around very  
> \> easily. So imagine you have these 2 functions defined:  
> \>  
> \> def firstWay(arg1, arg2)  
> \> return 'Tastes great.'  
> \>  
> \> def secondWay(arg1, arg2)  
> \> return 'Less filling.'  
> \>  
> \> Then you can define a method that takes a method name as an argument,  
> \> and calls it just by throwing parens (and any expected arguments of  
> \> course) after it.  
> \>  
> \> def doStuff(whichway, first\_arg, second\_arg)  
> \> return whichway(first\_arg, second\_arg)  
> \>  
> \> So calling:  
> \>  
> \> puts doStuff(firstWay, None, None)  
> \>  
> \> Would result in 'Tastes great.'  
> \>  
> \> What's the most graceful way to do this sort of thing in ruby? I  
> \> tried passing in e.g., firstWay.to\_proc, but that got me a complaint  
> \> about not having enough arguments on the call. I can imagine doing,  
> \> e.g.,  
> \>  
> \> first\_pointer = Proc.new do |foo, bar|  
> \> return firstWay(foo, bar)  
> \> end  
> \>  
> \> One for each alternate method & passing one of those in. I can also  
> \> imagine having doStuff take a block that would call the desired  
> \> function & yielding out to that. But neither of those are as pretty  
> \> as the python I think. Are those my best options in ruby, or is there  
> \> another way?  
> \>  
> \> Thanks!  
> \>  
> \> -Roy  
> \>  
> \>
> 
> Here is another way:
> 
> irb(main):001:0\> def dostuff(whichway, arg1, arg2)  
> irb(main):002:1\> send(whichway, arg1, arg2)  
> irb(main):003:1\> end  
> =\> nil

#dostuff is really superfluous because the way you defined it it's  
just an alias for #send.

> irb(main):004:0\> def first\_way(arg1, arg2)  
> irb(main):005:1\> "tastes great"  
> irb(main):006:1\> end  
> =\> nil  
> irb(main):007:0\> def second\_way(arg1, arg2)  
> irb(main):008:1\> "less filling"  
> irb(main):009:1\> end  
> =\> nil  
> irb(main):010:0\> dostuff(:first\_way, nil, nil)  
> =\> "tastes great"  
> irb(main):011:0\> dostuff(:second\_way, nil, nil)  
> =\> "less filling"  
> irb(main):012:0\> "No! #{dostuff(:first\_way, nil, nil).capitalize}!"  
> =\> "No! Tastes great!"

So basically you would just do

send(:first\_way, nil, nil)  
send(:second\_way, nil, nil)

However, if there are /many/ alternatives, then - depending on  
circumstances - a Hash might be a better alternative:

algorithms = {  
&nbsp;&nbsp;:first\_way =\> lambda {|a,b| 'Tastes great.'},  
&nbsp;&nbsp;:second\_way =\> lambda {|a,b| 'Less filling'},  
&nbsp;&nbsp;# more algorithms here...  
}  
# ...  
puts algorithms[:first\_way][nil, nil]  
puts algorithms[:second\_way][nil, nil]

Kind regards

robert

> **···**
>
> 2008/1/18, Justin Collins \<justincollins@ucla.edu\>:
> 
> > rpardee@gmail.com wrote:
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Roy\_Pardee](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Roy\_Pardee](https://rubytalk.org/u/Roy_Pardee)\
**Post date:** [18 January 2008 17:50 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/4 "2008-01-18T17:50:12Z")

</div>

Ah--that is nicer--thanks!

My momentary python envy (hmmm...) has now passed. 😉

-Roy

> **···**
>
> On Jan 17, 8:12 pm, Justin Collins \<justincoll...@ucla.edu\> wrote:
> 
> > rpar...@gmail.com wrote:  
> > \> Hey all,
> > 
> > \> Kind of an idle, syntax comparison question here...
> > 
> > \> In python, you can apparently pass method references around very  
> > \> easily. So imagine you have these 2 functions defined:
> > 
> > \> def firstWay(arg1, arg2)  
> > \> return 'Tastes great.'
> > 
> > \> def secondWay(arg1, arg2)  
> > \> return 'Less filling.'
> > 
> > \> Then you can define a method that takes a method name as an argument,  
> > \> and calls it just by throwing parens (and any expected arguments of  
> > \> course) after it.
> > 
> > \> def doStuff(whichway, first\_arg, second\_arg)  
> > \> return whichway(first\_arg, second\_arg)
> > 
> > \> So calling:
> > 
> > \> puts doStuff(firstWay, None, None)
> > 
> > \> Would result in 'Tastes great.'
> > 
> > \> What's the most graceful way to do this sort of thing in ruby? I  
> > \> tried passing in e.g., firstWay.to\_proc, but that got me a complaint  
> > \> about not having enough arguments on the call. I can imagine doing,  
> > \> e.g.,
> > 
> > \> first\_pointer = Proc.new do |foo, bar|  
> > \> return firstWay(foo, bar)  
> > \> end
> > 
> > \> One for each alternate method & passing one of those in. I can also  
> > \> imagine having doStuff take a block that would call the desired  
> > \> function & yielding out to that. But neither of those are as pretty  
> > \> as the python I think. Are those my best options in ruby, or is there  
> > \> another way?
> > 
> > \> Thanks!
> > 
> > \> -Roy
> > 
> > Here is another way:
> > 
> > irb(main):001:0\> def dostuff(whichway, arg1, arg2)  
> > irb(main):002:1\> send(whichway, arg1, arg2)  
> > irb(main):003:1\> end  
> > =\> nil  
> > irb(main):004:0\> def first\_way(arg1, arg2)  
> > irb(main):005:1\> "tastes great"  
> > irb(main):006:1\> end  
> > =\> nil  
> > irb(main):007:0\> def second\_way(arg1, arg2)  
> > irb(main):008:1\> "less filling"  
> > irb(main):009:1\> end  
> > =\> nil  
> > irb(main):010:0\> dostuff(:first\_way, nil, nil)  
> > =\> "tastes great"  
> > irb(main):011:0\> dostuff(:second\_way, nil, nil)  
> > =\> "less filling"  
> > irb(main):012:0\> "No! #{dostuff(:first\_way, nil, nil).capitalize}!"  
> > =\> "No! Tastes great!"
> > 
> > -Justin

---

<div class="post-metadata">

**Author:** ![Radoslaw\_Bulat](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Radoslaw\_Bulat](https://rubytalk.org/u/Radoslaw_Bulat)\
**Post date:** [18 January 2008 19:06 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/5 "2008-01-18T19:06:36Z")

</div>

In python when you use method name without parenthesises you get  
method reference. In Ruby it won't work beacuse method can be called  
without parenthesises (and Ruby programmers love it :)). If you want  
to get method reference you can use method(:you\_method\_name) and you  
get reference to Method instance. Later you can just call 'call'  
method and pass some parameters. Direct traslation of your program to  
Ruby looks like:

def firstWay(arg1, arg2)  
&nbsp;&nbsp;return 'Tastes great.'  
end

def secondWay(arg1, arg2)  
&nbsp;&nbsp;return 'Less filling.'  
end

def doStuff(whichway, first\_arg, second\_arg)  
&nbsp;&nbsp;return whichway.call(first\_arg, second\_arg)  
end

puts doStuff(method(:firstWay), nil, nil)

> **···**
>
> --  
> Radosław Bułat
> 
> [http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [19 January 2008 11:51 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/6 "2008-01-19T11:51:26Z")

</div>

Well that is good, but for the wrong reason ☹

Our Rubyists where cheating on you as you had a first class object  
called a function, and we were just using names.  
Now there are no functions in Ruby so we cannot pass them on, we do  
however have proc objects which would mimick what you have done in  
Python  
firstway =lambda{ |x| x+1}  
otherway = lambda{ |x| x - 1}  
def anyway away, anumber  
&nbsp;&nbsp;&nbsp;&nbsp;away.call anumber  
end

However the interesting and somewhat frustrating stuff starts with  
methods which are first class objects in Ruby  
class A  
&nbsp;&nbsp;&nbsp;&nbsp;def first x; x+1 end  
&nbsp;&nbsp;&nbsp;&nbsp;def second x; x-1 end  
&nbsp;&nbsp;&nbsp;&nbsp;@f = instance\_method :first  
&nbsp;&nbsp;&nbsp;&nbsp;@s = instance\_method :second  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :f, :s  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def any method, number  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;method.bind( self ).call number  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

p A.new.any(A.f, 41)  
p A.new.any(A.s,43)

Things are however a little bit trickier when it comes to binding  
instance\_methods  
look at this code  
class B  
&nbsp;&nbsp;&nbsp;def b; 42 end  
end

p B.instance\_method(:b).bind(Object.new).call # :(((  
I still fail to see the need for this restriction  
Now lots of trickery is needed if you want to do things like these

module M  
&nbsp;&nbsp;def m; 42 end  
end

p Object.new.send(:extend, M).m

If you are interested in this stuff you might have a look at the  
implementation of Traits in Ruby  
[http://rubyforge.org/projects/ruby-traits/](http://rubyforge.org/projects/ruby-traits/)  
HTH  
Robert

> **···**
>
> On Jan 18, 2008 6:50 PM, rpardee@gmail.com \<rpardee@gmail.com\> wrote:
> 
> > My momentary python envy (hmmm...) has now passed. 😉
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> Whereof one cannot speak, thereof one must be silent.  
> Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [18 January 2008 19:11 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/7 "2008-01-18T19:11:50Z")

</div>

Radosław Bułat wrote:

> In python when you use method name without parenthesises you get  
> method reference. In Ruby it won't work beacuse method can be called  
> without parenthesises (and Ruby programmers love it :)). If you want  
> to get method reference you can use method(:you\_method\_name) and you  
> get reference to Method instance. Later you can just call 'call'  
> method and pass some parameters. Direct traslation of your program to  
> Ruby looks like:
> 
> def firstWay(arg1, arg2)  
> &nbsp;&nbsp;return 'Tastes great.'  
> end
> 
> def secondWay(arg1, arg2)  
> &nbsp;&nbsp;return 'Less filling.'  
> end
> 
> def doStuff(whichway, first\_arg, second\_arg)  
> &nbsp;&nbsp;return whichway.call(first\_arg, second\_arg)  
> end
> 
> puts doStuff(method(:firstWay), nil, nil

Ah, that's the one I was looking for. Then you can do:

def doStuff(whichway, first\_arg, second\_arg)  
&nbsp;&nbsp;whichway[first\_arg, second\_arg]  
end

-Justin

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [19 January 2008 12:59 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/8 "2008-01-19T12:59:50Z")

</div>

> I still fail to see the need for this restriction

Without this restriction you can write

&nbsp;&nbsp;&nbsp;Array.instance\_method(:).bind(Hash.new).call(0)

and if you are not lucky ruby just crash.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [19 January 2008 10:45 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/9 "2008-01-19T10:45:07Z")

</div>

Yeah, but do you really consider

doStuff(method(:firstWay), nil, nil)

more concise than

send(:firstWay, nil, nil)

?

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 18.01.2008 20:11, Justin Collins wrote:
> 
> > Radosław Bułat wrote:
> > 
> > > In python when you use method name without parenthesises you get  
> > > method reference. In Ruby it won't work beacuse method can be called  
> > > without parenthesises (and Ruby programmers love it :)). If you want  
> > > to get method reference you can use method(:you\_method\_name) and you  
> > > get reference to Method instance. Later you can just call 'call'  
> > > method and pass some parameters. Direct traslation of your program to  
> > > Ruby looks like:
> > > 
> > > def firstWay(arg1, arg2)  
> > > &nbsp;&nbsp;return 'Tastes great.'  
> > > end
> > > 
> > > def secondWay(arg1, arg2)  
> > > &nbsp;&nbsp;return 'Less filling.'  
> > > end
> > > 
> > > def doStuff(whichway, first\_arg, second\_arg)  
> > > &nbsp;&nbsp;return whichway.call(first\_arg, second\_arg)  
> > > end
> > > 
> > > puts doStuff(method(:firstWay), nil, nil
> > 
> > Ah, that's the one I was looking for. Then you can do:
> > 
> > def doStuff(whichway, first\_arg, second\_arg)  
> > &nbsp;&nbsp;whichway[first\_arg, second\_arg]  
> > end

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [19 January 2008 13:24 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/10 "2008-01-19T13:24:35Z")

</div>

> \> I still fail to see the need for this restriction
> 
> Without this restriction you can write
> 
> &nbsp;&nbsp;&nbsp;Array.instance\_method(:).bind(Hash.new).call(0)
> 
> and if you are not lucky ruby just crash.

Bad example of mine, what disturbs me is the following

not working:  
Module::new{ def a; 42 end}.instance\_method(:a).bind(Object.new).call

not working:  
a=Modul::new{ def a; 42 end}.i\_m :a  
Object.module\_eval{ define\_method :x, a }

working;  
module M  
&nbsp;&nbsp;def m; 42 end  
end

Object.module\_eval{  
&nbsp;&nbsp;&nbsp;&nbsp;include M  
&nbsp;&nbsp;&nbsp;&nbsp;define\_method :x, M.instance\_method(:m)  
}  
p Object.new.x

and I thought we were all ducktypers ☹

Robert

> **···**
>
> On Jan 19, 2008 1:59 PM, ts \<decoux@moulon.inra.fr\> wrote:
> 
> > Guy Decoux
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> Whereof one cannot speak, thereof one must be silent.  
> Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [19 January 2008 19:45 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/11 "2008-01-19T19:45:06Z")

</div>

Robert Klemme wrote:

> **···**
>
> > On 18.01.2008 20:11, Justin Collins wrote:
> > 
> > > Radosław Bułat wrote:
> > > 
> > > > In python when you use method name without parenthesises you get  
> > > > method reference. In Ruby it won't work beacuse method can be called  
> > > > without parenthesises (and Ruby programmers love it :)). If you want  
> > > > to get method reference you can use method(:you\_method\_name) and you  
> > > > get reference to Method instance. Later you can just call 'call'  
> > > > method and pass some parameters. Direct traslation of your program to  
> > > > Ruby looks like:
> > > > 
> > > > def firstWay(arg1, arg2)  
> > > > &nbsp;&nbsp;return 'Tastes great.'  
> > > > end
> > > > 
> > > > def secondWay(arg1, arg2)  
> > > > &nbsp;&nbsp;return 'Less filling.'  
> > > > end
> > > > 
> > > > def doStuff(whichway, first\_arg, second\_arg)  
> > > > &nbsp;&nbsp;return whichway.call(first\_arg, second\_arg)  
> > > > end
> > > > 
> > > > puts doStuff(method(:firstWay), nil, nil
> > > 
> > > Ah, that's the one I was looking for. Then you can do:
> > > 
> > > def doStuff(whichway, first\_arg, second\_arg)  
> > > &nbsp;&nbsp;whichway[first\_arg, second\_arg]  
> > > end
> > 
> > Yeah, but do you really consider
> > 
> > doStuff(method(:firstWay), nil, nil)
> > 
> > more concise than
> > 
> > send(:firstWay, nil, nil)
> > 
> > ?
> > 
> > Cheers
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;robert
> 
> Clearly this is just a toy example to show options for how one might call methods using variable names. If the doStuff method actually did more stuff, then it would no longer be equivalent to send.
> 
> -Justin

---

<div class="post-metadata">

**Author:** ![Gregory\_Marton](https://avatars.discourse-cdn.com/v4/letter/g/858c86/32.png) [@Gregory\_Marton](https://rubytalk.org/u/Gregory_Marton)\
**Post date:** [24 February 2008 13:46 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/12 "2008-02-24T13:46:14Z")

</div>

What bugs me about ruby is that I can't do this  
def add\_one(n)  
&nbsp;&nbsp;n + 1  
end  
[3,6,8].map(add\_one)  
=\> [4,7,9]

I can't even do it more explicitly, because map won't take anything  
other than a block:

irb(main):001:0\> class A; def A.addone(x); x + 1; end; end  
=\> nil  
irb(main):002:0\> A.method(:addone)  
=\> #\<Method: A.addone\>  
irb(main):003:0\> [3,6,8].map{|x| x + 1}  
=\> [4, 7, 9]  
irb(main):004:0\> [3,6,8].map(A.method(:addone))  
ArgumentError: wrong number of arguments (1 for 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4:in `map'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4  
irb(main):005:0\> [3,6,8].map(Proc.new{|x| A.method(:addone).call(x)})  
ArgumentError: wrong number of arguments (1 for 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):5:in `map'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):5  
irb(main):006:0\> [3,6,8].map Proc.new{|x| A.method(:addone).call(x)}  
ArgumentError: wrong number of arguments (1 for 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6:in `map'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6

Any of these would be defeating the purpose of brevity anyway. The best  
I can do is to make a block that calls the proc I want:

irb(main):007:0\> [3,6,8].map{|x| A.method(:addone).call(x)}  
=\> [4, 7, 9]

or more simply:  
irb(main):008:0\> [3,6,8].map{|x| A.addone(x)}  
=\> [4, 7, 9]

this seems a waste of keystrokes, a cognitive indirection, and probably  
even a few processor cycles. Is there a way?

Grem

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Arlen\_Christian\_Mar1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Arlen\_Christian\_Mar1](https://rubytalk.org/u/Arlen_Christian_Mar1)\
**Post date:** [24 February 2008 13:55 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/13 "2008-02-24T13:55:11Z")

</div>

Hi there,

You can skip the between and do this:

> > def add\_one(n)  
> > n+1  
> > end

=\> nil

> > [3,6,8].map &method(:add\_one)

=\> [4, 7, 9]

Personally, I like doing it this way. You could always try monkey-patching  
(ooh, buzzword) Symbol by adding Symbol#to\_proc to do some method  
resolution. Not sure how well it'll work for you, but a guess.

Arlen

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [24 February 2008 14:03 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/14 "2008-02-24T14:03:35Z")

</div>

Gregory Marton wrote:

> What bugs me about ruby is that I can't do this  
> def add\_one(n)  
> n + 1  
> end  
> [3,6,8].map(add\_one)  
> =\> [4,7,9]

[3,6,8].map(&method(:add\_one))  
#=\> [4, 7, 9]

And yeah, it'd be nice if there was a more concise syntax than that, but  
that's what we've currently got. Thinking about it, you could do something  
like this if you want a shorter syntax:

class Symbol  
&nbsp;&nbsp;def -@()  
&nbsp;&nbsp;&nbsp;&nbsp;method(self)  
&nbsp;&nbsp;end  
end  
[3,6,8].map(&-:add\_one)

But that's kinda hackish.

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Arlen\_Christian\_Mar1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Arlen\_Christian\_Mar1](https://rubytalk.org/u/Arlen_Christian_Mar1)\
**Post date:** [24 February 2008 13:56 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/15 "2008-02-24T13:56:48Z")

</div>

Sorry, I missed your exact example. It works the same:

> > class A; def A.add\_one(n); n + 1; end; end

=\> nil

> > [3, 6, 8].map &A.method(:add\_one)

=\> [4, 7, 9]

> >

Cheers,  
Arlen.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 February 2008 14:00 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/16 "2008-02-24T14:00:53Z")

</div>

Oh you might do this even very often if you are writing Ruby1.8/1.9  
agnostic libraries.  
Cheers  
Robert

> **···**
>
> On Sun, Feb 24, 2008 at 2:55 PM, Arlen Cuss \<celtic@sairyx.org\> wrote:
> 
> > Hi there,
> > 
> > You can skip the between and do this:
> > 
> > \>\> def add\_one(n)  
> > \>\> n+1  
> > \>\> end  
> > =\> nil  
> > \>\> [3,6,8].map &method(:add\_one)  
> > =\> [4, 7, 9]
> > 
> > Personally, I like doing it this way. You could always try monkey-patching  
> > (ooh, buzzword) Symbol by adding Symbol#to\_proc to do some method  
> > resolution.
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> Whereof one cannot speak, thereof one must be silent.  
> Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 February 2008 21:34 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/17 "2008-02-25T21:34:55Z")

</div>

Not necessary. Here's another approach that has the added advantage that it will also work with 1.8 - and it's short and conscise:

irb(main):001:0\> add\_one = lambda {|x| x + 1}  
=\> #\<Proc:0x7ff9c4bc@(irb):1\>  
irb(main):002:0\> (1..3).map &add\_one  
=\> [2, 3, 4]  
irb(main):003:0\>

Although I have to say it's not clear to me what speaks against doing  
(1..3).map {|x| x + 1}.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 24.02.2008 14:55, Arlen Cuss wrote:
> 
> > You can skip the between and do this:
> > 
> > > > def add\_one(n)  
> > > > n+1  
> > > > end
> > 
> > =\> nil
> > 
> > > > [3,6,8].map &method(:add\_one)
> > 
> > =\> [4, 7, 9]
> > 
> > Personally, I like doing it this way. You could always try monkey-patching  
> > (ooh, buzzword) Symbol by adding Symbol#to\_proc to do some method  
> > resolution. Not sure how well it'll work for you, but a guess.

---

<div class="post-metadata">

**Author:** ![Gregory\_Marton](https://avatars.discourse-cdn.com/v4/letter/g/858c86/32.png) [@Gregory\_Marton](https://rubytalk.org/u/Gregory_Marton)\
**Post date:** [24 February 2008 14:05 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/18 "2008-02-24T14:05:17Z")

</div>

Robert Dober wrote:

> **···**
>
> > On Sun, Feb 24, 2008 at 2:55 PM, Arlen Cuss \<celtic@sairyx.org\> wrote:
> > 
> > > =\> [4, 7, 9]
> > > 
> > > Personally, I like doing it this way. You could always try monkey-patching  
> > > (ooh, buzzword) Symbol by adding Symbol#to\_proc to do some method  
> > > resolution.
> > 
> > Oh you might do this even very often if you are writing Ruby1.8/1.9  
> > agnostic libraries.  
> > Cheers  
> > Robert
> 
> Could you both elaborate? I'm not sure what it would mean to  
> "monkeypatch" this, or which differences with 1.9 will require it.  
> Thanks already: this is a bit C-like, but nicer than wrapping a block  
> around things.
> 
> Grem  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gregory\_Marton](https://avatars.discourse-cdn.com/v4/letter/g/858c86/32.png) [@Gregory\_Marton](https://rubytalk.org/u/Gregory_Marton)\
**Post date:** [24 February 2008 14:19 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/19 "2008-02-24T14:19:26Z")

</div>

Another potentially easy question:

I'd like to pass a surround method into another, where the surround  
method would take a block, and should yield to it, perhaps doing stuff  
before and after. This is not a real call/cc surround, just something  
simple. So:

def do\_things(options={})  
&nbsp;&nbsp;opts = {:surround =\> Proc.new{yield}}.merge(options)  
&nbsp;&nbsp;opts[:surround] do  
&nbsp;&nbsp;&nbsp;&nbsp;the actual task  
&nbsp;&nbsp;end  
end

except this doesn't actually work with Procs, because opts[:surround] is  
an undefined method, of course. It contains a method reference. Well,  
rather, a procedure reference. In any case, I want to invoke it with a  
block.

so I think  
&nbsp;&nbsp;y = lambda{ yield }  
&nbsp;&nbsp;y.call{ 3 }  
perhaps, but no:  
LocalJumpError: no block given

How shall I pass it a block?

Thanks,  
Grem

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 February 2008 15:13 UTC](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862/20 "2008-02-24T15:13:33Z")

</div>

> Robert Dober wrote:
> 
> \>\> =\> [4, 7, 9]  
> \>\>  
> \>\> Personally, I like doing it this way. You could always try monkey-patching  
> \>\> (ooh, buzzword) Symbol by adding Symbol#to\_proc to do some method  
> \>\> resolution.  
> \> Oh you might do this even very often if you are writing Ruby1.8/1.9  
> \> agnostic libraries.  
> \> Cheers  
> \> Robert
> 
> Could you both elaborate? I'm not sure what it would mean to  
> "monkeypatch" this, or which differences with 1.9 will require it.  
> Thanks already: this is a bit C-like, but nicer than wrapping a block  
> around things.

Symbol#to\_proc exists in Ruby1.9 standard, if you want to have  
programs run in Ruby1.8 too you will be forced to define your  
Symbol#to\_proc yourself.  
But I guess that I am wrong that you have to do that often, that was  
written with very little thinking done :(.

Robert

> **···**
>
> On Sun, Feb 24, 2008 at 3:05 PM, Gregory Marton \<q8pb97u001@sneakemail.com\> wrote:
> 
> > \> On Sun, Feb 24, 2008 at 2:55 PM, Arlen Cuss \<celtic@sairyx.org\> wrote:
> > 
> > Grem  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> Whereof one cannot speak, thereof one must be silent.  
> Ludwig Wittgenstein

[Next page](https://rubytalk.org/t/passing-method-references-in-python-ruby/43862.md?page=2)
