Ruby gives me heartburn

Ruby gives me thisDisclaimer: this is not meant to be a “Ruby sucks” post. It’s just me publically ranting about some pains I’ve had with Ruby recently.

Override everything

As Ruby Tips points out, In Ruby you can easily extend classes and modules. This means that nothing in Ruby (including built in classes and modules) are closed!
While admittedly, the hacker in me thinks this is really slick, the developer in me can’t stand it. I’ve lost so much time at work hunting down method definitions and overrides across an existing Ruby codebase. Maybe there’s a tool or IDE out there that would help make it more obvious where these methods are defined (currently I use a mix of GVim andTextPad), but hunting across dozens of Ruby scripts between RakefilesRuby on Rails scriptsCruiseControl.rb scripts, and custom Ruby code to find the actual method definition is not my idea of a good time.

Implicit returns

Again, as Ruby Tips points out, Value of the last expression in a method becomes the return value of the method. In Ruby return keyword is optional.
Being relatively new to Ruby, this was really confusing at first. For example, in the following Ruby method definition, what does the following method return if the regex matches?

def dummy_method
    input = "Hi there, I'm almost 30"
    if /almost (\d+)/ =~ input
        $1
    end
end

It’ll return “30”, obviously! 😉 Once you understand more about what Ruby is doing there it’s not as bad, but it took me a while to get the hang of implicit returns.

Helpful links

Some links that have helped me limp along through my Ruby experiences:

Leave a Reply

Your email address will not be published. Required fields are marked *