我感興趣的人會如何得到這個工作去:Ruby操作符可以被別名嗎?
me = "this is a string"
class << me
alias :old<< :<<
def <<(text)
old<<(text)
puts "appended #{text}"
end
end
我想,當某樣東西附加到me
變量,對象將使用重新定義的方法。
如果我嘗試運行這個,我得到syntax error, unexpected ':', expecting kEND
在:<<
。
OP看上去很不開心:<< – Nakilon 2013-07-01 01:17:52