Wednesday, February 20, 2013

Ruby chaining and comments

I have run across interesting feature in ruby when I tried to write chain methods separated in new lines. Ruby 1.9+ enables dots in new line at the beginning of command, not only at the end of last line, like this:

"somestring"
  .upcase
  .first

But, if you want to put comment in separate line before chain method call then ruby throws syntax error. So, this is invalid expression in ruby:

"somestring"
  .upcase
  #take first letter
  .first

If you want this to be valid you have to put dots at the end of the line.

"somestring".
  upcase.
  # take first letter
  first

No comments:

Post a Comment