环境:Ruby 2.0.0
class HelloWorld
def initialize(myname = "Ruby")
@name = myname
end
def hello
puts "Hello, world. I am #{@name}."
end
def name=(ddsa = "testname")
puts ddsa
@name = ddsa
end
end
ruby = HelloWorld.new
ruby.hello
ruby.name=()
ruby.hello
输出的结果是
Hello, world. I am Ruby.
#空行
Hello, world. I am .
以我的理解来看,应该是输出
Hello, world. I am Ruby.
testname
Hello, world. I am testname.
我知道定义访问实例变量可以用attr_accessor :name
刚初学 ruby,希望各位能帮忙解释下,谢。