Why Ruby is not my favourite programming language

Actually it is my favourite. But the title comes from this article (which itself was inspired and copied from the famous paper about pascal by Brian Kernighan).

On with business though – Ruby has some serious wrinkles that produce counter-intuative and plain strange behaviour. One might say that they violate the Principle of Least Surprise. But POLS is somewhat personal – surprising to whom? (just me maybe!)

Surprising result #1

b=1=> 1a if a=bNameError: undefined local variable or method `a' for main:Object        from (irb):2

This is odd indeed. It seems that the Ruby parser thinks that a is a method at the beginning of the line. But when it parses a=b it will know (and remember) that it is a variable. However, when the condition passes and it comes to execute ‘a’, it does not apply its new found knowledge about the nature of a – it still goes for the method. And hence the error.

Surprising result #2

c=1 unless defined? c=> nilc=> nil

This is a bit easier to explain. Once the parser has seen c=1 it enters c in the symbol table, it knows that c exists as a variable. Hence it is defined (as nil). So c=1 is never executed.

Surprising result #3

class A  attr_accessor :a  def b    @a=0    x=a    a=x+1    puts("a is #{a}")    puts("@a is #{@a}")  endend=&gt nilc = A.new=> #<A:0x32fb7c>c.ba is 1@a is 0

No mystery here really – it’s a nice gotcha though – a= does not call self.a=, it just assigns a local variable. It is just a little confusing unless you think carefully.

Surprising result #4

[0.0, 1/0.0].sort=> [0.0, Infinity][0.0, 0/0.0].sortArgumentError: comparison of Float with Float failed

Thanks to Evan Weaver for pointing this out to me. This is due to 0/0.0 returning the value NaN (as opposed to 1/0.0 which returns Infinity). And the comparison with NaN by sort gives the strange error message.

Let me know if you spot any more oddities.


3 thoughts on “Why Ruby is not my favourite programming language

  1. "Why Ruby is not my favourite programming language"so what is your favorite prog lang now? I mean, the article suggests that you dropped ruby as yr favvy because of the 4^H 3^H 2.. maybe even one oddity mentioned above.back to php? java? perl?p.s.: i think yr picking a catchy title for your blog posts but secretly still using ruby when you need to get things done..

  2. @malcontent: you are right about the warning, but a if (a=1) works no better, at least with 1.8.7:<pre>irb(main):001:0> a if (a=1)(irb):1: warning: found = in conditional, should be ==NameError: undefined local variable or method `a’ for main:Object
    from (irb):1</pre>@cies breijs: Read the first sentence of my post! (Actually it *is* my favourite)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s