What are your favorite Ruby WTFs? Oddities, peculiarities, tripping hazards, bits of code that do NOT return what you expect them to return?

6 comments

  1. @avdi setting local variables with `foo = …` when I mean to set instance variables with `self.foo = …`. Been using the language nearly 20 years and this asymmetrical weirdness tripped me up again this week.

  2. @avdi one of my favorite “that’s so clever it’s ingenious” was `ubygems.rb` being a file in stdlib that just did a `require “rubygems”`. This cleverness powered the `ruby -rubygems` of old.

  3. @avdi These days mine are mostly precedence-related

    foo(bar) + 1
    vs
    foo (bar) + 1

    And for those times I need to store the result of a method call which is used in a conditional to use it inside the body:

    if (x = foo(lol)) || bar
    vs
    if x = foo(lol) || bar

Comments are closed.