Ruby : What happens to dependencies when method is extracted out as Proc

It is clear that whenever you try to save any method bound to any object; it is extracted out as an instance to Method class. People may be in misconception that methods are extracted out as instance of Proc.

What about the dependencies viz. instance variables and Other object instance?

Since, no new object is created; the same Method object defined within the object is referenced, so no dependency issue shall occur.  See the code below to understand what  I was trying to say.

> class Test
>   def print
>     puts "the word is #{@word}"
>   end
>   def set_word=(word)
>     @word = word
>   end
> end

015 > newTest = Test.new
=> #<Test:0x000000028c7740>  
    016 > newTest.set_word = "elephant"
=> "elephant"
017 > method = newTest.method(:print)
=> #<Method: Test#print>  
    018 > method.call
the word is elephant
=> nil
019 > method.class
=> Method
021 > method.class.ancestors
=> [Method, Object, Kernel, BasicObject]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s