我有一个具有以下关系的消息模型:
belongs_to :sender, Class: "User"
belongs_to :recipient, Class: "User"
在某些情况下,我正在尝试使用 class_eval 覆盖接收者方法。
这有效:
def update_recipient(message, recipient_addition = nil)
message.class_eval <<-EVAL
def recipient
"test"
end
EVAL
end
message.recipient => "测试"
但是,这不会:
def update_recipient(message, recipient_addition = nil)
message.class_eval <<-EVAL
def recipient
[#{message.recipient}, #{recipient_addition}]
end
EVAL
end
(eval):3: syntax error, unexpected keyword_end, expecting ']'