Ruby: Extremely Unsafe

I have been making the same mistake a few times recently: instead of writing
things.each do |thing| ... end
Where things is a method, not a variable, I would write:
things do |thing| ... end
which has the end result of doing nothing. And I would have a hard time tracking down the bug - even when I have tests written which are failing because of it. I think it's because you just don't see a mistake like this, it's just one of those things where you can stare at the line all day and still not have a clue. Admittedly, this is a stupid mistake, but I also blame ruby for being extremely lose in its method invocation behavior when dealing with blocks. A block on a method is like an optional argument on ALL methods, not just the ones where you mean to use blocks. So this mean you can pass a block to any method, even if it's as simple as:
def hello
  'hello'
end
I have enumerated a handful of other languages including Smalltalk, Python, Haskell, Lisp, Java, and none of them behave this way. This is just one example of why Ruby is on the extremely unsafe end of the language spectrum. And this is also why there's a bigger need for automated testing in Ruby software, because the intepreter is so forgiving that even little mistakes like these - which are normally easily caught at runtime, if not compile time - can go through undetected.

Another unsafe thing about Ruby software is the frequent use of hashes as the poor man's named parameters. If you misspell the name of the key, there would be no complaint. And when you find out something's wrong, you wouldn't know where to look first.

Yet another is the fact that arrays simply return nil when you give it an out-of-bounds index:
>> [][7]
=> nil

I guess being unsafe is just part of the Ruby culture. In related news: Javascript is extremely unsafe too, arguably even more so than Ruby, since, for example it's not strict about enforcing the number of parameters you can pass into a function..

blog comments powered by Disqus