Created
January 10, 2015 22:39
-
-
Save lnznt/c25a49b559c2fb2d5641 to your computer and use it in GitHub Desktop.
Ruby: インスタンス変数とクラス変数 ref: http://qiita.com/lnznt/items/4f71e57c9bd1a520e49a
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
$ ruby sample-ivar.rb | |
foo は呼ばないでください. | |
foo は呼ばないでください. | |
foo は呼ぶなッ!!3 度目!! | |
-------- | |
foo は呼ばないでください. | |
foo は呼ばないでください. | |
foo は呼ぶなッ!!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
$ ruby sample-cvar.rb | |
foo は呼ばないでください. | |
foo は呼ばないでください. | |
foo は呼ぶなッ!!3 度目!! | |
-------- | |
foo は呼ぶなッ!!4 度目だ!! | |
foo は呼ぶなッ!!5 度目だ!! | |
foo は呼ぶなッ!!6 度目だ!! |
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
$ ruby sample.rb | |
[PM, D, M, MM, C, B, A, M0, Object, Kernel, BasicObject] | |
:D # メソッド getv の出力 | |
"I am 'PM'" # メソッド foo の出力 |
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
D -> C -> B -> A -> M0 -> M -> MM -> PM # クラス変数参照の順番 |
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 | |
def foo | |
@@count ||= 0 | |
@@count += 1 | |
if @@count < 3 | |
puts "foo は呼ばないでください." | |
else | |
puts "foo は呼ぶなッ!!#{@@count} 度目!!" | |
end | |
end | |
end | |
c1 = C.new | |
c1.foo | |
c1.foo | |
c1.foo | |
puts '-' * 8 | |
c2 = C.new | |
c2.foo | |
c2.foo | |
c2.foo |
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 | |
def foo | |
@count ||= 0 | |
@count += 1 | |
if @count < 3 | |
puts "foo は呼ばないでください." | |
else | |
puts "foo は呼ぶなッ!!#{@count} 度目!!" | |
end | |
end | |
end | |
c1 = C.new | |
c1.foo | |
c1.foo | |
c1.foo | |
puts '-' * 8 | |
c2 = C.new | |
c2.foo | |
c2.foo | |
c2.foo |
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
digraph { | |
rankdir=LR; | |
node [shape = box]; | |
node [color = blue] A B C D; | |
node [color = orange, peripheries = 2] M0 M MM PM; | |
D [style = filled, fillcolor = cyan]; | |
D -> C [color = blue]; | |
C -> B [color = blue]; | |
B -> A [color = blue]; | |
PM -> D [color = green]; | |
D -> M [color = green]; | |
M -> MM [color = green]; | |
MM -> C [color = green]; | |
C -> B [color = green]; | |
B -> A [color = green]; | |
A -> M0 [color = green]; | |
} |
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
module M0 | |
@@v = :M0 | |
def foo ; "I am 'M0'" ; end | |
end | |
module MM | |
@@v = :MM | |
def foo ; "I am 'MM'" ; end | |
end | |
module M | |
include MM | |
@@v = :M | |
def foo ; "I am 'M'" ; end | |
end | |
module PM | |
@@v = :PM | |
def foo ; "I am 'PM'" ; end | |
end | |
class A | |
include M0 | |
@@v = :A | |
def foo ; "I am 'A'" ; end | |
end | |
class B < A | |
@@v = :B | |
def foo ; "I am 'B'" ; end | |
end | |
class C < B | |
@@v = :C | |
def foo ; "I am 'C'" ; end | |
end | |
class D < C | |
include M | |
prepend PM | |
@@v = :D | |
def foo ; "I am 'D'" ; end | |
def getv | |
@@v | |
end | |
end | |
d = D.new | |
p d.class.ancestors #=> [PM, D, M, MM, C, B, A, M0, Object, Kernel, BasicObject] | |
p d.getv | |
p d.foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment