On Sun, May 4, 2014 at 5:51 PM, Quintus <quintus / quintilianus.eu> wrote:
>
> My bad, answered to the wrong thread. Sorry for the inconvenience.
>
> - - - - Original reply follows - - - - - -
>
> Maybe you are looking for #lambda?
>
> ----------------------------------------
> anon_method = lambda do |x|
>   x + 2
> end
>
> anon_method.call(4) #=> 6
> ----------------------------------------

This would be the way to go. One can also invoke the method via #[]:

irb(main):001:0> anon_method = lambda do |x|
irb(main):002:1*   x + 2
irb(main):003:1> end
=> #<Proc:0x00000600418f60@(irb):1 (lambda)>
irb(main):004:0> anon_method[9]
=> 11

Basically with that approach anything that implements #[] or #call can be used:

irb(main):005:0> Pluser = Struct.new :add do
irb(main):006:1* def call(x) x + add end
irb(main):007:1> alias [] call
irb(main):008:1> end
=> Pluser
irb(main):009:0> f = Pluser.new 2
=> #<struct Pluser add=2>
irb(main):010:0> f[9]
=> 11
irb(main):011:0> f.call(9)
=> 11

Sorry for the bad class naming. :-)

Kind regards

robert


-- 
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/