# PartialInvocation sample

**URL:** <https://www.ruby-forum.com/t/partialinvocation-sample/135949>\
**Category:** Ruby\
**Created:** [April 26, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/partialinvocation-sample/135949 "2008-04-26T20:42:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [April 26, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/partialinvocation-sample/135949/1 "2008-04-26T20:42:41Z")

</div>

Hi,

I created a partial invocation example below, which I think can be  
useful in many cases. Since ruby 1.9 introduced curry as a syntax suger,  
perhaps a generic and consistent partial invocation can be given a  
thought.

# A partial invocation example

# 

# Define a dummy object @@\_ and define a kernel method \_ to return that

object

# Modify method\_missing in Object to treat method whose name ends with

‘\_’ as a partial invocation

# 

# Partial invocation is like obj.do\_this\_ a, \_, c

# 

# Benefit: intuitive partial invocation, can work with IDE’s

auto-completion.

# Issue: people can use xxx\_ as a real method name, which causes

confusion

class A  
def do\_this a, b, \*c  
puts "========== do\_this: ", a, b, c  
end

def self.do\_that a, b, \*c  
puts "========== do\_that: ", a, b, c  
end  
end

A.new.do\_this 1, 2, 3, 4  
A.do\_that 1, 2, 3, 4

module Kernel  
@@\_ = Object.new  
def \_  
@@\_  
end  
end

class Object  
alias method\_missing\_old method\_missing

def method\_missing method, \*args, &block  
if method.to\_s[-1,1] == ‘_’  
lambda{|\*a|  
new\_args = []  
args.each\_with\_index do |arg, i|  
if arg === \_  
if i \< args.length - 1  
new\_args.push a.shift  
else  
new\_args.push \*a  
end  
else  
new\_args.push arg  
end  
end  
new\_method = method.to\_s.chomp '_’  
send new\_method, \*new\_args, &block  
}  
else  
method\_missing\_old method, \*args, &block  
end  
end  
end

A.new.do\_this\_(1, _, 3, 4)[2]  
A.do\_that_(1, \_, 3, 4)[2]

A.new.do\_this\_(_, , 3, 4)[1, 2]  
A.do\_that(_, \_, 3, 4)[1, 2]

A.new.do\_this\_(1, 2, 3, 4)[]  
A.do\_that\_(1, 2, 3, 4)[]

do\_this\_ = A.new.do\_this\_(1, 2, _)  
do\_this_[3]  
do\_this\_[3, 4]

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [April 26, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/partialinvocation-sample/135949/2 "2008-04-26T22:15:22Z")

</div>

On Apr 26, 2:42 pm, Guoliang C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> object  
> class A  
> A.do\_that 1, 2, 3, 4
> 
> ```
> end
> 
> ```
> 
> end
> 
> ## do\_this\_ = A.new.do\_this\_(1, 2, _) do\_this_[3] do\_this\_[3, 4]
> 
> Posted viahttp://www.ruby-forum.com/.

Hi,

#\_ is sort off limits because IRB uses it. However, #\_\_ is usable, and  
that is what Facets’ library uses.

While the notation is nice, I’m not sure is necessary. If #curry is  
extended to take slot-order as arguments, then curry itself can be  
used. Eg.

aproc = Proc.new{ |a,b| a \*\* b }

aproc.curry =\> lambda{ |a| lambda{ |b| a \*\* b }}

aproc.curry(1) =\> lambda{ |b| lambda{ |a| a \*\* b }}

More complex…

aproc = Proc.new{ |a,b,c,d| …}

aproc.curry(3,1,0) =\> lambda{ |d| lambda{ |b| lambda{ |a| lambda{ |  
c|…

Note, that last slot (2) is implied and need not be specified in the  
arguments.

T.

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [April 27, 2008, 6:16am UTC](https://www.ruby-forum.com/t/partialinvocation-sample/135949/3 "2008-04-27T06:16:24Z")

</div>

Trans wrote:

> Hi,
> 
> #\_ is sort off limits because IRB uses it. However, #\_\_ is usable, and  
> that is what Facets’ library uses.

I’m not familiar with how \_ is used in IRB. Can you please explain a bit  
more?

> While the notation is nice, I’m not sure is necessary. If #curry is  
> extended to take slot-order as arguments, then curry itself can be  
> used. Eg.
> 
> aproc = Proc.new{ |a,b| a \*\* b }
> 
> aproc.curry =\> lambda{ |a| lambda{ |b| a \*\* b }}
> 
> aproc.curry(1) =\> lambda{ |b| lambda{ |a| a \*\* b }}
> 
> More complex…
> 
> aproc = Proc.new{ |a,b,c,d| …}
> 
> aproc.curry(3,1,0) =\> lambda{ |d| lambda{ |b| lambda{ |a| lambda{ |  
> c|…
> 
> Note, that last slot (2) is implied and need not be specified in the  
> arguments.
> 
> T.

I agree curry can be made more generic. It’ll be even better if we can  
have partial invocation on method calls. I think most of the time procs  
are local to a method and we can substitute known values into proc body  
without currying. However partial method invocation can be very useful  
to reduce entering same literal values.

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [April 27, 2008, 6:24am UTC](https://www.ruby-forum.com/t/partialinvocation-sample/135949/4 "2008-04-27T06:24:57Z")

</div>

Hi,

On Sun, Apr 27, 2008 at 2:16 PM, Guoliang C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m not familiar with how \_ is used in IRB. Can you please explain a bit  
> more?

\_ is the last returned value:

$ irb

> > \_  
> > =\> nil  
> > 42  
> > =\> 42  
> > \_  
> > =\> 42

Arlen
