What's "new" in Ruby
While updating Delayed Job’s YAML parser compatibility last week, Steve and I came across a Ruby method that neither of us had seen before, Class.allocate. Given that we were in the trenches of serializing and deserializing objects, this method caught our attention.
So, what’s it do? It allocates space for a new object of the class, but doesn’t call the
initialize method on that new instance. Whoa… wait, what? You can create an object that skips the initialization of that object? Yes… but don’t think you’re going to go off and start using
allocate willy nilly. In my eyes, it’s main use is in serialization, where you may want to split the allocation and initialization of the object in two steps.
Nonetheless, here’s a quick example showing the difference between
class Person def initialize(*args) @name = "Brian" @loves_coffee = true end def loves_coffee? @loves_coffee || false end def name @name end end > brian = Person.new # => # > brian.name # => "Brian" > brian.loves_coffee? # => true > b = Person.allocate # => # > b.name # => nil > b.loves_coffee? # => false
As you can see in the second group of results, when calling
Person.allocate we’re given back a new instance of
Person but without the initialized attributes. What’s interesting, though, is under the hood Ruby’s
Class.new first calls
What are some other lesser known ways of creating or initializing objects?