attr_accessorでクラス変数を見る
attr_accessorで、@から始まる変数を見るときにちょっと迷った。
attr_accessor :@x, :@yという書き方だと、
in `attr_accessor': invalid attribute name `@x' (NameError)
が出てしまう。
class Example attr_accessor :x,:y,:z def initialize(x, y) @x = y @y = x @z = 15 z = 20 end end a = Example.new(5,3) p a.x#=>3 p a.y#=>5 p a.z#=>15
あれ、普通に見れる。でも:zは@zが優先されてる。