Let's Simplify Ruby

I love Ruby, but there are a lot of things that are in the language I’ve never used.

For example, did you know about the “flip flop operator”[1]?

(1..20).each do |x|
  puts x if (x == 5) .. (x == 10)
end

I certainly didn’t, and nobody in our office had seen or used it.

Yesterday, Steve wrote about the shorthand string interpolation syntax. Nobody in our office had heard about this until maybe a year ago, and none use it.

Today, I am suggesting we deprecate each_with_index from Ruby. Why? Because we now have with_index that you can chain with each, map, select and more!

I realized each_with_index was redundant as I was teaching Ruby. Lots of people ask if there’s a map_with_index or similar. If you start by teaching people each.with_index (note the . instead of _) they will guess they can use it with map and instinctively know they can do more.

Another great idea is to extract REXML out of Ruby and into a gem.

I think Ruby can benefit from removing some things. We all benefit from a simpler language, and while it will never get as small as I might want, we should always think about what we could live without.

So what else can we remove? I have a few more ideas, but I’d love to hear your suggestions (and I expect some of you to think I’m nuts).

If you have good ideas, submit them as issues to Ruby[2] and start the discussion. We may not get to simplify as much as we want, but it is important to talk about it.

[1] Thanks to Nithin Bekal who’s great post on flip flop helped me understand it. RubySpec’s code also explains it well.

[2] Thanks to Richard Schneeman for prodding me to submit issues instead of complaining.

Photo of Daniel Morrison

Daniel founded Collective Idea in 2005 to put a name to his growing and already full-time freelance work. He works hard writing code, teaching, and mentoring.

Comments

  1. November 28, 2014 at 9:04 AM

    The strings interpolation thing sucks, I used to play with them back in 2004, it was actually one of the firsts things I learned from the Pickaxe. It was probably before 2005 that I found them controversial and stopped using them. I was convinced there were depecreted, reading Steve’s article made me feel nostalgia :P

    About flip-flop… c’mon! Who may want them removed? haha. They are awesome, they can be cryptic sometimes, but they are really useful. Regexps are cryptic too but they worth it. I think people is usually afraid of having non-trivial expressions. I know Ruby is about readability and self explained code but having a couple of flip-flop’s like magic here and there it’s not that bad.

    REXML, damn aweful pice of tool. But we may still need an XML parser anyway, HTTP support must stay so a tool to parse responses. You may also find some (usually) useless stdlibs like CSV or RSS. I thinks that, as long as they keep them modularized, it’s not that bad to have them there. One thing that they should care about is naming convention. They break every single pattern you may find! Ma y be not move them as GEMS but include them in a Gem way (naming, modularization, code distribution…).