I am working with a ruby hash
that contains key/value pairs for creating a new subscriber in my MailChimp API.
user_information = {
'fname' => 'hello world',
'mmerge1' => 'Product X' if user.product_name.present?
}
Obviously, I am getting a syntax error of syntax error, unexpected modifier_if
... I am basically only wanting to add the mmerge1
based upon the conditional being true.
You can't use if
that way, inside a hash-initialization block. You'll have to conditionally add the new key/value after initializing the hash:
user_information = {
'fname' => 'hello world',
}
user_information['mmerge1'] = 'Product X' if user.product_name.present?
user_information = {'fname' => 'hello world'}
user_information.merge!({'mmerge1' => 'Product X'}) if user.product_name.present?
#=> {"fname"=>"hello world", "mmerge1"=>"Product X"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With