Skip to content

Instantly share code, notes, and snippets.

@deepak
Created October 13, 2010 10:17
Show Gist options
  • Save deepak/623787 to your computer and use it in GitHub Desktop.
Save deepak/623787 to your computer and use it in GitHub Desktop.
ruby metaprogramming usage using define_method and undef_method.txt
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