Created
June 12, 2018 14:55
-
-
Save tzmfreedom/c761cd3229ac52811ff3ef02be0ae9c6 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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