Last active
September 18, 2017 15:31
-
-
Save hiroykam/c6a109527783904b317a79ef4216878f to your computer and use it in GitHub Desktop.
Rubyのインスタンス変数・メソッドとクラス変数・メソッドを検証 ref: http://qiita.com/hiroykam/items/9b47ca3fc05afbf91903
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 Test | |
| @c_i_var = 0 | |
| @@c_var = 10 | |
| var = 'var' | |
| def initialize | |
| @i_var = 100 | |
| end | |
| def test | |
| @i_var += 1 | |
| @@c_var += 1 | |
| self | |
| end | |
| def examine1 | |
| p 'Test::examine1' | |
| p @i_var, @c_i_var, @@c_var | |
| self | |
| end | |
| def Test.examine2 | |
| @c_i_var += 1 | |
| p 'Test::examine2' | |
| p @i_var, @c_i_var, @@c_var | |
| Test.new | |
| end | |
| def examine3 | |
| Test | |
| end | |
| p 'Test::var' | |
| p var | |
| end | |
| class Test2 < Test | |
| def Test2.examine2 | |
| p 'Test2::examine2' | |
| p @i_var, @c_i_var, @@c_var | |
| end | |
| end | |
| t = Test.new | |
| t.test.examine1 | |
| Test.examine2.test.examine1.examine3.examine2 | |
| Test2.examine2 |
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
| "Test::var" | |
| "var" | |
| "Test::examine1" | |
| 101 | |
| nil | |
| 11 | |
| "Test::examine2" | |
| nil | |
| 1 | |
| 11 | |
| "Test::examine1" | |
| 101 | |
| nil | |
| 12 | |
| "Test::examine2" | |
| nil | |
| 2 | |
| 12 | |
| "Test2::examine2" | |
| nil | |
| nil | |
| 12 |
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 Test | |
| @@c_var = 2 | |
| def initialize(val) | |
| @i_var = val | |
| end | |
| def inspect | |
| '%d, %d' % [@@c_var, @i_var] | |
| end | |
| def examine1 | |
| @i_var += 2 | |
| self | |
| end | |
| def examine2 | |
| @@c_var += 2 | |
| self | |
| end | |
| end | |
| t1 = Test.new(3) | |
| t1.examine1.examine2 | |
| p t1 | |
| p Test.new(3) | |
| puts '--- shallow copy ---' | |
| t2 = t1.clone | |
| t2.examine1.examine2 | |
| p t1 | |
| p t2 | |
| p Test.new(3) | |
| puts '--- deep copy ---' | |
| t3 = Marshal.load(Marshal.dump(t1)) | |
| t3.examine1.examine2 | |
| p t1 | |
| p t2 | |
| p t3 | |
| p Test.new(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
| 4, 5 | |
| 4, 3 | |
| --- shallow copy --- | |
| 6, 5 | |
| 6, 7 | |
| 6, 3 | |
| --- deep copy --- | |
| 8, 5 | |
| 8, 7 | |
| 8, 7 | |
| 8, 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment