1 min read

Ruby WTF?!

Ruby WTF?!

The second law of Kris Buytaert states that the number of WTF's per second, is the most important metric to chart...

If we would do that, the chart would have been steeper than the rise of the Bitcoin the last few days.

Watch this:

irb(main):001:0> s="this_is_an_example"
=> "this_is_an_example"
irb(main):002:0> s.sub(/_/,'\_')
=> "this\\_is_an_example"
irb(main):003:0> s.sub(/_/,"\_")
=> "this_is_an_example"
irb(main):004:0> s.sub(/_/,"\\_")
=> "this\\_is_an_example"
irb(main):005:0> s.sub(/_/,'\\_')
=> "this\\_is_an_example"
irb(main):006:0> f = '\_'
=> "\\_"
irb(main):007:0> f
=> "\\_"
irb(main):008:0> f.inspect
=> "\"\\\\_\""
irb(main):009:0> f = "\_"
=> "_"
irb(main):010:0> f.inspect
=> "\"_\""

WTF Ruby?!

The answer is that what you see, is not some Ruby funkyness. It's irb playing tricks with your mind: irb always shows you the "inspect" output, which replaces certain characters in strings.

irb(main):015:0> rs = "a\\_second_example"
=> "a\\_second_example"
irb(main):016:0> puts rs
a\_second_example