26

After having read http://www.seejohncode.com/2012/03/16/ruby-class-allocate/ and looking more into the allocate method: http://www.ruby-doc.org/core-1.9.3/Class.html#method-i-allocate I became very curious.

Ruby was built in a way that we did not have to manually allocate or free space for/with objects, but we are given the ability to do so. Why?

What are the uses in Ruby of allocating Objects manually? The article I read showed a custom initialize method, but are the uses of it so limited?

2
  • 2
    Serialization systems (such as Marshal) need to be able to create an object without calling initialize since initialize might do expensive or unnecessary things when you're just going to overwrite everything. Oct 25, 2012 at 1:58
  • Mu, that's really interesting. Can you give additional examples or a more in depth explanation? Oct 25, 2012 at 5:39

1 Answer 1

31

The main reason allocate exists is to allow you to build custom constructors for your objects. As the article you linked mentioned, you can envision the SomeClass.new method as doing something like the following by default:

class SomeClass
  def self.new(*a, &b)
    obj = allocate

    # initialize is a private instance method by default!
    obj.send(:initialize, *a, &b) 
  end
end

Despite what the documentation says, the existence of the allocate method is not so much about memory management as it is about providing some finer grained control over the object creation lifecycle. Most of the time, you won't need this feature, but it is useful for certain edge cases.

For example, in the Newman mail framework, I used this technique to implement a fake constructor for a TestMailer object; it implemented the new method for API compatibility, but actually returned a single instance regardless of how many times it was called:

class Newman::TestMailer
  def self.new(settings)
    return self.instance if instance

    # do some Mail gem configuration stuff here

    self.instance = allocate
  end

  attr_accessor :instance
end

I've not seen many other use cases apart from redefining new as shown above (although I imagine that some weird serialization stuff also uses this feature). But with that in mind, it's worth pointing out that Ruby consistently provides these kinds of extension points, regardless of whether or not you'll need to use them regularly. Robert Klemme has a great article called The Complete Class which I strongly recommend reading if you want to see just how far this design concept has been taken in Ruby :-)

5
  • 2
    Thank you, I especially like the real-world example you gave as well. Nov 7, 2012 at 21:40
  • I think the example should say cattr_accessor :instance at the end. ...Also, I wonder if the same thing could be achieved by calling super instead of allocate. Jun 24, 2014 at 13:45
  • Tried it. super works! But you then need an initialize method to accept the settings argument. (Incidentally: while code like this is sometimes unavoidable to preserve backward compatibility, it may be surprising to a user that the settings are completely ignored the second time around!) Jun 24, 2014 at 13:55
  • from what i understand up until now, allocate is used to create an object of a class, and from some examples i see, it is used to create method that creates object as alternative of new method. So is it basically just like overload constructor in C# and Java? Jan 8, 2016 at 9:35
  • 3
    Singleton could be used for your use case, no? Aug 20, 2016 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.