次の関係を持つメッセージモデルがあります。
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 => "test"
ただし、これはしません。
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 ']'