Method Calls in Ruby

After watching Google Tech Talks – Monads and Gonads , I found some interest in functional programming concepts. Though I haven’t been able to understand the monad concept, just played around some language features of ruby functional programming, or meta-programming.

class String
  def get_binding
    binding()
  end
end

puts eval('"abc".length')
 # => "3"
puts eval("length", "abc".get_binding)
 # => "3"
puts "abc".send("length")
 # => "3"
puts "abc".method(:length).call
 # => "3"
puts "abc".instance_exec(){ self.length }
 # => "3"
puts String.instance_method(:length).bind("abc").call
 # => "3"
Advertisements

Posted on January 27, 2013, in Ruby. Bookmark the permalink. Leave a comment.

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

%d bloggers like this: