Ruby Headscratcher- 인스턴스 변수는 출력되지 않습니다
-
03-07-2019 - |
문제
나는 아마 어리석은 일을하고 있지만 그것이 무엇인지 알 수 없습니다.
이 프로그램에서보고있는 출력은입니다
foo
test
내가 기대하는 것은입니다
foo
abc
test
여기서 분명히 잘못된 것을 본 사람이 있습니까?
class Foo
def initialize(l)
@label = l
end
def label
@label
end
def abc
@abc
end
def abc=(abc)
@abc
end
end
foo = Foo.new("foo")
foo.abc=("abc")
puts foo.label
puts foo.abc
puts "test"
해결책
당신은 당신에게 @abc를 설정하지 않았습니다 abc=
방법. 모양이되어야합니다
def abc=(val)
@abc = val
end
다른 팁
오류가 무엇인지 알려주는 Logan의 답변 외에도 다음은 오류가 처음부터 결코 발생하지 않았을 것입니다.
class Foo
def initialize(l)
@label = l
end
attr_reader :label
attr_accessor :abc
end
제휴하지 않습니다 StackOverflow