Codefol.io logo book

Unsolvable Ruby Problems: Array#map on an Array subclass, but keep the subclass!

Jul 18 2012

I love looking at problems with “no solution” in languages to stretch my mind. Here’s a fun one from Ruby.

In Ruby, there’s not a good way to map over a subclass of Array and get the same subclass back.

That is, if you start with this code:

class Array
  def but_doubled
    map { |x| x * 2 }
  end
end

This will work fine on an Array, but if you call myarraysubobject.butdoubled, you’ll get a plain Array, not a subclass object.

Yes There Is, You Meanie!

You may think, “sure, no problem. Just re-define #map on the subclass to return the subclass.” Ah, but then you don’t get to choose whether to return the subclass or Array. Plus, if the author of the subclass didn’t do that you can’t do it… Or you have to monkeypatch. This is Ruby.

But if you don’t know what subclass you might be passed, you have a problem. That happens sometimes, even in code like Rails, and then the solutions aren’t always pretty.

Make a New One

You could create a new object of that subclass and then add the objects back into that. That works, right? Well, sometimes.

If you don’t know the subclass, you don’t know what initialize() takes as parameters. You can hope that it’s the same as Array, but it often isn’t…

Indeed, you don’t have to ask long before people start telling you to avoid this entirely and rethink your whole approach.

Then… What?

I don’t have a perfect answer – nor does Rails, as you saw above.

But after thinking of several ways to solve the problem and coming up with a few of your own, do you now know more about Ruby?

Basically I mostly just like messing around in the guts of Ruby. Do you?

What things are impossible in your favorite language? What are your favorite half-solutions?

Topics
Articles by Topic

a simple chalk face
About Me

RSS icon
RSS

Twitter logo in chalk
Twitter

Sign Up to Get Emailed Articles and Free Chapters.
* indicates required
If you sign up, I'll email you about software and how to improve at it. I promise to send you what I wish I had known.

Posts I Like

Where Do I Put My Code in Rails?
Why Do They Say Rails Doesn't Scale?
BigCo New Employee Training: Inside Voice
When Should You Not Use Heroku?
What's Deployment vs Provisioning vs Orchestration?
What is Rack? A Primer