Last active
September 30, 2016 14:16
-
-
Save mkurtikov/bc0e98db51e6c9fcce34b59d2d4e733a to your computer and use it in GitHub Desktop.
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 A | |
| def initialize() | |
| @a = 1 | |
| end | |
| end | |
| a = A.new | |
| a.instance_variable_get(:@a) # 1 | |
| a.instance_variable_set(:@a, 2) | |
| a.instance_variable_get(:@a) # 2 | |
| a.instance_variable_set(:@b, 3) # 3 | |
| a.instance_variable_defined? :@b # true | |
| a.instance_variable_get(:@b) # 3 |
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
| eval('p 33') # 33 | |
| class Demo | |
| def initialize(n) | |
| @secret = n | |
| end | |
| def binding | |
| binding | |
| end | |
| end | |
| k1 = Demo.new(99) | |
| b1 = k1.binding | |
| k2 = Demo.new(-3) | |
| b2 = k2.binding | |
| eval("@secret", b1) #=> 99 | |
| eval("@secret", b2) #=> -3 | |
| eval("@secret") #=> nil |
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 A | |
| def initialize | |
| @a = 1 | |
| end | |
| end | |
| a = A.new | |
| a.instance_eval { p self } # #<A:0x007fd1c194c150 @a=1> | |
| a.instance_eval { p @a } # 1 | |
| b = 99 | |
| a.instance_eval { @a = b } | |
| a.instance_eval { p @a } # 99 | |
| a.instance_exec(10) { |b| @a += b } | |
| a.instance_exec { p @a } # 109 |
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 B | |
| end | |
| b = B.new | |
| B.class_eval do | |
| def b | |
| 'b' | |
| end | |
| end | |
| b.b # b | |
| B.class_exec('hello') do |method_name| | |
| define_method method_name do | |
| p method_name | |
| end | |
| end | |
| b.hello # hello | |
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 A | |
| def initialize(a) | |
| @a = a | |
| end | |
| def plus9 | |
| p @a + 9 | |
| end | |
| end | |
| a1 = A.new(1) | |
| a2 = A.new(2) | |
| plus9 = a1.method :plus9 # <Method: A#plus9> | |
| plus9.call # 10 | |
| uplus9 = plus9.unbind # <UnboundMethod: A#plus9> | |
| plus9 = uplus9.bind a2 #<Method: A#plus9> | |
| plus9.call # 11 | |
| class B | |
| def initialize | |
| @a = 91 | |
| end | |
| end | |
| b = B.new | |
| uplus9.bind b # TypeError: bind argument must be an instance of A | |
| B.send(:define_method, :b_plus9, &plus9) | |
| b.b_plus9 # 11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment