Peter's Blog

Redefining the Impossible

Ruby Notes 4


Syntactic Sugar

Ruby has some perlisms such as 'unless'. This makes it possible to write code as follows:

return unless bFlag

Without unless one may have to resort to:

if !bFlag
  return
end

I think code is easier to read when lines start with keywords, when the keywords are further inside a line one has to rely on syntax highlighting to make the keyword shout out at you. This is especially important with a keyword like return where you have to be aware that the code in the lines following may not be executed and understand the circumstances in which this will apply.

This way of expressing a statement always seems harder for me to get my head around, having written in C like languages for years. If one wrote similar expressions in English, which seems to be better expressed?

  • jump up and down and sing a merry song if you are happy
  • if you are happy then jump up and down and sing a merry song

To me the second form is even clearer as having the if at the start warns you in advance that a condition is coming up, it's not a surprise half way through the sentence. However, the second form requires you to type a whole extra word, and a four letter one at that!


Filed under: noob ruby

Stasigr Says:

Hello, very nice site, keep up good job! Admin good, very good.

Have Your Say

I welcome constructive comments or questions but I reserve the right to delete any comments that displease me.

Who are you?

(Optional) If you enter an email address here I might email you back. Your email address will not be sold to spammers or shown anywhere

What do you have to say?