Last active
August 29, 2015 14:17
-
-
Save thash/0d8c5e7bd42a88537871 to your computer and use it in GitHub Desktop.
AllAbout 031
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 C | |
| attr_reader :bind | |
| def initialize(val) | |
| @bind = binding | |
| end | |
| end | |
| c = C.new(999) | |
| c.val | |
| #=> undefined method `val' for #<C:0x007fef31888bc8 @bind=#<Binding:0x007fef31888b78>> (NoMethodError) | |
| # initialize内のコンテキストが保持されているので、valにアクセスできる | |
| p c.bind #=> #<Binding:0x007fb50a088b28> | |
| eval('puts val', c.bind) #=> 999 |
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 Hoge | |
| end | |
| # インスタンスメソッドを定義 | |
| Hoge.class_eval("def dice; puts #{rand(1000)} end") | |
| h = Hoge.new | |
| h.dice # => 532, など | |
| Hoge.class_eval do | |
| (0..10).map{|i| 2**i }.each do |n| | |
| attr_accessor :"val#{n}" | |
| end | |
| end | |
| h2 = Hoge.new | |
| p h2.val1024 # => nil | |
| p h2.val1024 = 3 # => 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
| p Gem.constants | |
| #=> [:VERSION, :GEM_PRELUDE_SUCKAGE, :RubyGemsVersion,...] |
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
| 2.upto(9) do |i| | |
| define_method "mul#{i}" do |val| | |
| i * val | |
| end | |
| end | |
| p mul2(3) # => 6 | |
| p mul9(3) # => 27 |
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 'puts "hoge"' # => hoge | |
| 2.upto(9) do |i| | |
| eval <<-CODE | |
| def mul#{i}(val) | |
| #{i} * val | |
| end | |
| CODE | |
| end | |
| p mul2(3) # => 6 | |
| p mul9(3) # => 27 |
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 X | |
| def initialize(val) | |
| @val = val | |
| end | |
| end | |
| x = X.new(12345) | |
| # アクセサを定義してないのでこれでは呼び出せないが... | |
| puts x.val | |
| #=> undefined method `val' for #<X:0x007f914d088898 @val=12345> (NoMethodError) | |
| # x内のコンテキストで評価すれば@valを参照できる | |
| x.instance_eval('puts @val') # => 12345 | |
| # ブロックで書くことも出来る | |
| x.instance_eval do | |
| puts @val * 2 | |
| end # => 24690 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment