Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
class Some
def initialize(n); @n=n; end
def smth; puts @n; end
end
some1 = Some.new(27); some1.smth #=> prints "27", returns nil
class Some
alias :smth_else :smth
def smth; smth_else; puts "overriding original smth method"; end
end
some1.smth
# returns nil, and prints:
# 27
# overriding original smth method
class Some
save_smth = instance_method(:smth)
define_method :smth do
save_smth.bind(self).call
puts "whatever"
end
end
some1.smth
# returns nil, prints:
# 27
# overriding original smth method
# whatever
# method alias can be still invoked
some1.smth_else #=> prints "27", returns nil
some2 = Some.new(82); some2.smth_else #=> prints "82", returns nil
# suggested by Robert Klemme (http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/405189)
class Some; private :smth_else; end
some1.smth_else
# gives:
# NoMethodError: private method `smth_else' called for #<Some:0x92d7c98 @n=27>
class Some
def new_method; smth_else; end
end
some1.new_method #=> 27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.