3

I have a function that returns a hash, given certain parameters:

build_message = function(from, to, bcc, subject, tag, htmlbody, textbody, replyto) {
    message = {"From": from,
        "To": to,
        "Subject": subject,
        "HtmlBody": htmlbody,
        "TextBody": textbody};
    message.encode();
}

Some of these parameters, like bcc and replyto are optional. If the caller provides null values for them, they must not be present in the hash I return. That is to say, "Bcc": bcc must only be present in the hash if the bcc argument is non-null.

Here is my first attempt, but the parser doesn't like it (this goes right before the message.encode() line of the function):

bcc_body = bcc => {"Bcc": bcc} | {};
message.put(bcc_body);

Is using the put() operation allowed on a variable inside a function like this? If so, is something wrong with my syntax?

Steve Nay
  • 2,819
  • 1
  • 30
  • 41

1 Answers1

2

You can use .put() inside a function, but remember that it returns a new hash and leaves the original unmodified.

Try ending your method like this:

bcc_body = bcc => {"Bcc": bcc} | {};
newmessage = message.put(bcc_body);
newmessage.encode();
TelegramSam
  • 2,770
  • 1
  • 17
  • 22
  • 1
    Can I chain them? For example: `newmessage = message.put(first_hash).put(second_hash).put(third_hash);` – Steve Nay Apr 05 '11 at 02:59
  • yes, chaining works. remember that you can also put an expression that resolves to a hash instead of a hash itself: newmessage = message.put(bcc => {"Bcc": bcc} | {}); – TelegramSam Apr 05 '11 at 22:47