저는 setter 역할을하는 메서드를 작성하려고하는데 할당 된 값 외에도 추가 인수가 필요합니다. 바보 예 :Ruby에서 추가 인수를 사용하는 setter 메서드 만들기
class WordGenerator
def []=(letter, position, allowed)
puts "#{letter}#{allowed ? ' now' : ' no longer'} allowed at #{position}"
end
def allow=(letter, position, allowed)
# ...
end
end
인덱서 작품으로 작성하고 내가 이런 식으로 호출 할 수 있습니다 : 나는 다음 중 하나를 시도 할 때
gen = WordGenerator.new
gen['a', 1] = true
# or explicitly:
gen.[]=('a', 1, true)
을하지만, 인터프리터는 불평 :
gen.allow('a', 1) = false # syntax error
gen.allow=('a', 1, false) # syntax error
왜이 작동하지 않습니다, 나는 분명한가 누락 되었습니까?
관련/중복 : http://stackoverflow.com/questions/9280623/setter-method-assignment-with-multiple-arguments – kotique