I have a Message model that has the following relationships:
belongs_to :sender, Class: "User"
belongs_to :recipient, Class: "User"
I’m attempting to use class_eval to overwrite the recipient method in certain cases.
This works:
def update_recipient(message, recipient_addition = nil)
message.class_eval <<-EVAL
def recipient
"test"
end
EVAL
end
message.recipient => “test”
However, this doesn’t:
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 ']'
The first
#is misinterpreted as a comment character, discarding the rest of the line. The#{}are expected to be interpolated inside double quotes, though there doesn’t seem to be a reason to put these in#{}right now as they are just simple string values.… unless you’re planning something like: