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
Comments are closed.
Avdi Grimm, Code Cleric
What are your favorite Ruby WTFs? Oddities, peculiarities, tripping hazards, bits of code that do NOT return what you expect them to return?
Comments are closed.
@avdi HereDocs were surprising the first time I ran into them and to this day I have to look up how they work every time I use them.
@rebelwarrior @avdi I’ve internalized their usage, but chaining methods onto the _start_ of a heredoc was/is a big WTF for colleagues.
<<~STR.chomp
no new line
STR
@avdi
https://ruby.social/@tom/109392442461787560
@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.
@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.
@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