Created
October 13, 2010 10:17
-
-
Save deepak/623787 to your computer and use it in GitHub Desktop.
ruby metaprogramming usage using define_method and undef_method.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
puts "define_method: #{defined?(define_method).inspect}" | |
define_method(:bar) { puts "one" } | |
end | |
a = Foo.new | |
a.bar | |
class Foo | |
class << self | |
undef_method :define_method #why whis works | |
end | |
# undef_method :define_method # and this does not | |
puts "define_method: #{defined?(define_method).inspect}" | |
end | |
class Foo | |
puts "define_method: #{defined?(define_method).inspect}" | |
define_method(:baz) { puts "two" } | |
rescue | |
puts "define_method is undefined" | |
end | |
# on irb | |
# self.class.send(:define_method, :foo) { puts "foo" } | |
# irb is running as a special TopLevel object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment