Skip to content

Instantly share code, notes, and snippets.

@tzmfreedom
Created June 12, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzmfreedom/c761cd3229ac52811ff3ef02be0ae9c6 to your computer and use it in GitHub Desktop.
Save tzmfreedom/c761cd3229ac52811ff3ef02be0ae9c6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Hoge
def foo
a = 123
(class << self; self; end).class_eval do
define_method(:bar) do
puts a
end
end
end
def baz
a = true
singleton_class.class_eval do
define_method(:qux) do
puts a
end
end
end
end
h = Hoge.new
h.foo
h.bar # => 123
h.baz
h.qux # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment