Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Modules Store Inventory Using Modules Extend and Include

What is the function/origin of the word "new" in "object = new(attributes)" in the ClassMethods module?

Typically in the lessons we've seen "new" when initializing a new class: "dog = Dog.new(name)"

Seeing it here throws me off a bit.

Thanks

5 Answers

Ulfar Ellenarson
Ulfar Ellenarson
5,277 Points

HI Brendan,

The object = new(attributes) calls the initialise method of the class that the module is mixed in with and returns it so that it can be assigned to the object. For example the class Shirt includes the module ClassMethods The class Shirt has attr_accessor : attributes and therefore we can call the create method from the module ClassMethods on an instance of the Shirt Class on the instance variable attribute (:name, :size). I admit that I am still a little confused that the attr_accessor becomes an array but that is being done in the ClassMethods module. We first create an instance using the method create from module ClassMethods in the Shirt Class ie. shirt1 = Shirt.create(name: "MTF", size: "L")

For added thought include makes the module's methods available to the instance of a class, while extend makes these methods available to the class itself.

If anybody can shed more light on this matter it would be appreciated.

Hi Brendan,

we've used .new in the context of creating a new instance of an already existing class. I think in this lesson, "new" is just a variable created to pass the arguments onto and then assign this variable to the variable "object" which then holds the arguments you passed into the method. Maybe someone can underline what i just said because i'm not quite sure either. Hope i helped you a bit :)

Ulfar Ellenarson
Ulfar Ellenarson
5,277 Points

Hi Brendan,

I found the answer in stackflow. http://stackoverflow.com/questions/10383535/in-ruby-whats-the-relationship-between-new-and-initialize-how-to-return-n

There are important differences between the two methods.

new is a class method, which generally creates an instance of the class (this deals with the tricky stuff like allocating memory that Ruby shields you from so you don't have to get too dirty).

Then, initialize, an instance method, tells the object to set its internal state up according to the parameters requested.

Either of these can be overridden depending on what you want. For example, Foo.new might actually create and return an instance of FooSubclass if it needs to be clever enough to do that.

However, often it's better to delegate use cases like these to other class methods which are more explicit about what they do, for example Foo.relating_to(bar). Breaking other peoples expectations about what methods like new should do will confuse people more than it will help them in the long run.

As an example, look at the implementation of Singleton, a module which allows only one instance of a particular class to exist. It makes the new method private, and exposes an instance method which either returns the existing instance of the object, or calls new if it hasn't been created yet.

Ulfar Ellenarson
Ulfar Ellenarson
5,277 Points

Sorry I was to quick. I think what I wanted to show is in the Class documentation. new(args, ...) → obj click to toggle source Calls allocate to create a new object of class’s class, then invokes that object’s initialize method, passing it args. This is the method that ends up getting called whenever an object is constructed using .new.

Donghai Zhang
seal-mask
.a{fill-rule:evenodd;}techdegree
Donghai Zhang
Front End Web Development Techdegree Student 7,036 Points

I think in object = new(attributes) what we truly put is object = self.new(attributes), just as if we instantiate an object such as object = Shirt.new(attribute). The point is that since the context is the class itself, what self is dong is no need at all, that's why it makes puzzled.