我正在学习Ruby Proc课程。我不明白为什么执行“def state =”方法。
我也想知道为什么“t1.state = 1”结束了“def state =(1)”
我不明白“def state”和“def state =”之间的区别?
我可以理解这个连接“& proc> proc> proc.call”。
# -*- coding: utf-8 -*-
class Terminal
def initialize
@connected = []
@current_state = nil
end
def connect(&proc)
@connected << proc
end
def current_input_terminal(terminal)
connect do |hoge|
terminal.state = hoge
end
end
def state=(current)
if current != 0 && current != 1
raise(ArgumentError, "input 0 or 1")
end
if @current_state != current
@current_state = current
@connected.each do |i|
i.call(current)
end
end
end
def state
@current_state
end
end
t1 = t2 = Terminal.new()
t1.current_input_terminal(t2)
t1.state = 1
puts(t2.state,t1.state)
答案 0 :(得分:0)
这个表达式:
Terminal.new.state = 1
将调用方法def state=(current)
这个表达
puts Terminal.new.state
将调用方法def state
或读取数据