Forum: Ruby In Ruby everything are Objects except Functions / Methods?

4b1da7279bbee36eeb1cf77ece56e1bb?d=identicon&s=25 Addis Aden (Guest)
on 2014-06-30 18:00
(Received via mailing list)
Hiii,

i am new to this ruby-talk list, so i hope my question is not annoying
(especially for my not so goood english).

My question is simple. Is there a way to store a method in a variable? I
know that every method can be called with the keyword and send, but is
there a way to make it direct callable?

As example:

def addition(a, b)
    a + b
end

x = addition
x(2, 3) # => 5

Is there a method like partial or anything like this in ruby?

best wishes,

Adrian

___
@addisaden
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2014-06-30 18:08
(Received via mailing list)
On Mon, Jun 30, 2014 at 5:58 PM, Addis Aden <addis.aden@gmail.com>
wrote:
>
> def addition(a, b)
>     a + b
> end
>
> x = addition
> x(2, 3) # => 5
>
> Is there a method like partial or anything like this in ruby?
>

2.0.0p195 :001 > def addition(a, b)
2.0.0p195 :002?>       a + b
2.0.0p195 :003?>   end
2.0.0p195 :006 > x = method(:addition)
 => #<Method: Object#addition>
2.0.0p195 :010 > x.call 2, 3
 => 5

The standalone method is still bound to the object:

2.0.0p195 :011 > class A
2.0.0p195 :012?>   def initialize a
2.0.0p195 :013?>     @a = a
2.0.0p195 :014?>     end
2.0.0p195 :015?>   def addition y
2.0.0p195 :016?>     @a + y
2.0.0p195 :017?>     end
2.0.0p195 :018?>   end
 => nil
2.0.0p195 :019 > a = A.new 3
 => #<A:0x000000026024b0 @a=3>
2.0.0p195 :022 > x = a.method :addition
 => #<Method: A#addition>
2.0.0p195 :023 > x.call 4
 => 7

You can also get an unbound method, but you can only call it after
binding it to an object of the same class (kind_of):

2.0.0p195 :026 > u = x.unbind
 => #<UnboundMethod: A#addition>
2.0.0p195 :027 > u.call 4
NoMethodError: undefined method `call' for #<UnboundMethod: A#addition>

2.0.0p195 :028 > b = u.bind(A.new 12)
 => #<Method: A#addition>
2.0.0p195 :029 > b.call 4
 => 16

Hope this helps,

Jesus.
Abdb670e1c130f96f947a94d03c02efa?d=identicon&s=25 Eric Christopherson (echristopherson)
on 2014-06-30 18:11
(Received via mailing list)
On Mon, Jun 30, 2014, Addis Aden wrote:
> Hiii,

Hi!

>
> i am new to this ruby-talk list, so i hope my question is not annoying
> (especially for my not so goood english).

Not at all. As you might have seen from other messages today, we're
hungry for some actual Ruby discussion.

>
> My question is simple. Is there a way to store a method in a variable? I
> know that every method can be called with the keyword and send, but is
> there a way to make it direct callable?

Yes. Functions and methods (they're both the same construct really) can
be held in objects of the Method class, which are obtained by calling
`obj.method(:method_name)`. Omit the receiver for functions/global
methods like `puts`.

> Is there a method like partial or anything like this in ruby?
See this snippet:

    def addition(a, b)
      a + b
    end

    x = method(:addition)
    puts x.call(2, 3)
    puts x[2, 3] # syntax sugar; however, note [] instead of ()

    class Foo
      attr_accessor :a

      def initialize(a)
        self.a = a
      end

      def addition(b)
        self.a + b
      end
    end

    # For methods inside of classes:
    f = Foo.new(2)
    x = f.method(:addition)
    puts x.call(3)
    puts x[3]
4b1da7279bbee36eeb1cf77ece56e1bb?d=identicon&s=25 Addis Aden (Guest)
on 2014-07-01 10:51
(Received via mailing list)
Its that easy. Wow, thank you. never heard of the method method :D

Seems that the Method than works like a Proc? I guess that the only
difference is, that a Proc isnt bound to an object ^^


2014-06-30 18:11 GMT+02:00 Eric Christopherson
<echristopherson@gmail.com>:
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-07-01 11:38
(Received via mailing list)
On Tue, Jul 1, 2014 at 10:50 AM, Addis Aden <addis.aden@gmail.com>
wrote:
> Its that easy. Wow, thank you. never heard of the method method :D

:-)

> Seems that the Method than works like a Proc? I guess that the only
> difference is, that a Proc isnt bound to an object ^^

A Proc is a closure which a method is not. A Method only catches self
i.e. the object which will receive the call.  An UnboundMethod does
not even do that.

A closure captures variables from the surrounding scope

$ ruby -e 'x = 9; f = lambda {|y| x + y};p f.call(7)'
16

... which can even be changed:

$ ruby -e 'x = 9; f = lambda {|y| x + y};p f.call(7);x = 100; p
f.call(7)'
16
107

Kind regards

robert
4b1da7279bbee36eeb1cf77ece56e1bb?d=identicon&s=25 Addis Aden (Guest)
on 2014-07-01 12:59
(Received via mailing list)
Ohh, scope matters :) Thank you for the hint ^^

This topic gives a new perspective for a lot ^^


2014-07-01 11:37 GMT+02:00 Robert Klemme <shortcutter@googlemail.com>:
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2014-07-02 03:02
> Is there a method like partial or anything like this in ruby?

Yes, Proc#curry():


p = Proc.new do |x, y, z|
  x + y + z
end

new_p = p.curry[10, 20]
puts new_p[30]

--output:--
60
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.