PartialInvocation sample

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]

On Apr 26, 2:42 pm, Guoliang C. [email protected] 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.

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.

Hi,

On Sun, Apr 27, 2008 at 2:16 PM, Guoliang C. [email protected] 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