Thursday, May 08, 2014

Ruby 2.1.0 some useful changes

return in lambda
def yield_me
 yield
end

def run
  yield_me(&lambda {return "call from lambda"})
  "call from run method"
end

run   #=> "call from run method"
The example above would have returned "call from lambda" in  Ruby  previous ruby versions
---------------------------------------------------------------------------------------------------------------
#singleton_class?
class Test
  singleton_class?     #=> false
  class << self
    singleton_class?   #=> true
  end
end

Object#singleton_method
class Test
  def self.one
  end

  def two
  end
end

# returns class method
Test.singleton_method(:one)    #=> #
# doesn't return instance method
Test.singleton_method(:two)   #=> #-----------------------------------------------------------------------------------------------------------------
Method#original_name
To return the un-aliased name.
class Example
  def foo
    "foo"
  end
  alias bar foo
end

example = Example.new
example.method(:foo).original_name            #=> :foo
example.method(:bar).original_name            #=> :foo
Example.instance_method(:bar).original_name   #=> :foo




For more changes please visit here: 

https://github.com/ruby/ruby/blob/v2_1_0/NEWS