__call__ equivalent?

Does Ruby have an equivalent of Python's __call__ method?

Not sure what Python's __call__ does, but ...

meth.call(args, ...) => obj
  meth[args, ...] => obj

Invokes the meth with the specified arguments, returning the method’s return value.
    m = 12.method("+")
    m.call(3) #=> 15
    m.call(20) #=> 32

···

On Aug 3, 2004, at 10:31 AM, Kevin Howe wrote:

Does Ruby have an equivalent of Python's __call__ method?

---------

prc.call(params,...) => obj
  prc[params,...] => obj

Invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics. Generates a warning if multiple values are passed to a proc that expects just one (previously this silently converted the parameters to an array).

  For procs created using Kernel.proc, generates an error if the wrong number of parameters are passed to a proc with multiple parameters. For procs created using Proc.new, extra parameters are silently discarded.

  Returns the value of the last expression evaluated in the block. See also Proc#yield.
    a_proc = Proc.new {|a, *b| b.collect {|i| i*a }}
    a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
    a_proc[9, 1, 2, 3] #=> [9, 18, 27]
    a_proc = Proc.new {|a,b| a}
    a_proc.call(1,2,3)

  produces:
    prog.rb:5: wrong number of arguments (3 for 2) (ArgumentError)
     from prog.rb:4:in `call'
     from prog.rb:5

Kevin Howe said:

Does Ruby have an equivalent of Python's __call__ method?

Yes and no.

If you are asking: Can you define a call operator in a class so that
instances of that can pretend to be called with a function-like syntax?

Then the answer is no. Mainly because there is no "call" operator in Ruby.

If you are asking: Can you define objects that act like functions (ie.
functors).

Then then answer is yes. Proc and Continuations are objects that act like
functions. To call them, you send them a "call" message with the
appropriate arguments. To define your own callable object, just implement
the "call" method. Also the "[]" operator is often an alias for call.

···

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

Looking into it a bit more, Ruby's "method_missing" was what I was looking
for. A method which is triggered when an unknown method is called.

ref: http://www.rubycentral.com/book/ref_c_object.html#Object.method_missing

"Kevin Howe" <khowe@perfnet.ca> wrote in message
news:JWPPc.14624$NTD.11104@news04.bloor.is.net.cable.rogers.com...

···

Does Ruby have an equivalent of Python's __call__ method?

Does Ruby have an equivalent of Python's __call__ method?

Forgot to mention:
2.send '+', 3 #=> 5

···

On Aug 3, 2004, at 10:51 AM, Charles Mills wrote:

On Aug 3, 2004, at 10:31 AM, Kevin Howe wrote:

Not sure what Python's __call__ does, but ...

meth.call(args, ...) => obj
meth[args, ...] => obj

Invokes the meth with the specified arguments, returning the method’s return value.
   m = 12.method("+")
   m.call(3) #=> 15
   m.call(20) #=> 32

---------

prc.call(params,...) => obj
prc[params,...] => obj

Invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics. Generates a warning if multiple values are passed to a proc that expects just one (previously this silently converted the parameters to an array).

For procs created using Kernel.proc, generates an error if the wrong number of parameters are passed to a proc with multiple parameters. For procs created using Proc.new, extra parameters are silently discarded.

Returns the value of the last expression evaluated in the block. See also Proc#yield.
   a_proc = Proc.new {|a, *b| b.collect {|i| i*a }}
   a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
   a_proc[9, 1, 2, 3] #=> [9, 18, 27]
   a_proc = Proc.new {|a,b| a}
   a_proc.call(1,2,3)

produces:
   prog.rb:5: wrong number of arguments (3 for 2) (ArgumentError)
    from prog.rb:4:in `call'
    from prog.rb:5

If you are asking: Can you define a call operator in a class so that
instances of that can pretend to be called with a function-like syntax?
Then the answer is no. Mainly because there is no "call" operator in

Ruby.

That was what I was looking for, thanks!