Sunday, November 21, 2010

Ruby: Whitespace significant?

Many people claim that they do not like Python because of the significance of whitespace. Well, whitespace is also significant in Ruby. E.g.
>> def say
>>  puts 'hi'
>> end
=> nil
>> say
hi
=> nil
>> def say puts 'hi' end
SyntaxError: (irb):5: syntax error, unexpected tSTRING_BEG, expecting ';' or '\n'
def say puts 'hi' end
              ^
(irb):5: syntax error, unexpected keyword_end, expecting $end
        from /Users/cdunn2001/bin/irb:12:in `
' >> def say; puts 'hi'; end => nil >> say hi => nil
See? The newline is a substitute for the semicolon, not equivalent to a space or tab. This is not pedantry. To me, the main value of whitespace-independence is that you can insert the code into something else -- e.g. an HTML template -- without breaking it.

I wouldn't mind if I could use curly brackets instead of 'end', but that works only for blocks, not function definitions. So I wish that Ruby fans would quit bragging that their language is whitespace-independent. There are such languages, e.g. Perl, where whitespace merely delimits tokens and can be removed completed by relying on parentheses and other delimiters. Ruby is not one of them.

I do understand the objection to Python's syntax. It's not the enforced indentation; it's the lack of an 'end' delimiter. The result is that copy-and-paste operations can introduce mistakes. I get that.

I'm just sayin' ...

No comments:

Post a Comment