我在理解符号如何在我的代码中工作时有点麻烦。我明白它们基本上是不可变的字符串,但我不完全理解符号如何自动“识别”我的代码的其他部分。红宝石 - 了解符号
例如,在下面的程序中,我将两个方法对象传递给我的math_machine方法,但为此,我使用表示其名称的符号。 Ruby如何知道我指的是这些方法?
def plus x,y
return x+y
end
def minus x,y
return x-y
end
def math_machine(code,x,y)
code.call(x,y)
end
puts math_machine(method(:plus),5,5)
puts math_machine(method(:minus),5,5)
我不明白的是关于封装符号的另一个例子 - 如何attr_reader
,attr_writer
,并attr_accessor
知道后面的符号是指一个实例变量在我的计划?
如果有人能够向我解释Ruby中符号的神秘性质(幕后发生了什么),那真是太棒了!
你已经阅读[这个问题](http://stackoverflow.com/questions/2341837/understanding-symbols-in-ruby)? – 2011-06-08 21:35:56