You Can’t Subclass Integers in Ruby

This post is mainly just an excuse to test a Gist plugin for WordPress.

Occasionally, you might think it would be handy to subclass Numeric types such as Integer. For instance, you might want to create a constrained integer which can only have certain values.

Ruby isn’t quite turtles all the way down, though: Numerics in Ruby are immediates, meaning that they don’t represent a heap-allocated object. Since you can’t allocate them, you can’t create a subclass and allocate instances of the subclass: