Reputation: 7739
I am beginner to ruby. I want to fix mistake in one of mailing software. Code with fixes I appiled is below:
@headers[:recipient] = {
"To" => (cc.map do |p|
if not p.full_address == "[email protected]"
p.full_address
end),
"Cc" => [],
}
Unfortunately, that gives my these errors:
home/tymon/mmm/lib/mmm/modes/reply_mode.rb:103: syntax error, unexpected ')', expecting keyword_end (SyntaxError)
end),
^
/home/tymon/mmm/lib/mmm/modes/reply_mode.rb:104: syntax error, unexpected =>, expecting keyword_end
"Cc" => [],
I do not know the reasons of them. I read Ruby documentation about blocks and it should work well. Please suggest me something.
Upvotes: 0
Views: 52
Reputation: 19879
I don't think end)
is allowed. It needs to be one it's own line or at least have a semicolon after it. I'd be more inclined to use braces though:
@headers[:recipient] = {
"To" => (cc.map { |p|
if not p.full_address == "[email protected]"
p.full_address
}),
"Cc" => [],
}
And I'd probably change your logic to the inline version and compact it to avoid any nil entries:
@headers[:recipient] = {
"To" => (cc.map { |p| p.full_address == "[email protected]" ? nil : p.full_address}.compact),
"Cc" => [],
}
Update As @sawa mentioned '(begin; end)' is valid. Looking at it again I realize it's complaining because the 'if' isn't 'end'ed.
@headers[:recipient] = {
"To" => (cc.map do |p|
if not p.full_address == "[email protected]"
p.full_address
end
end),
"Cc" => [],
}
Still, I like my middle style best.
Upvotes: 1