Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Modules Store Inventory Using Modules Class Set Up

where the name and the size comes from? we just initialize the attribute "attributes"

hey here in this video we just initilize the "attributes" attribute, so how we use the name and the size when we create a new instance of the shirt class? I am missing something here, I also see that when I inspect the new instance, I get a hash of the attributes

1 Answer

David O' Rojo
David O' Rojo
11,050 Points

In the video, Jason used a shorthand to write a Hash omitting the brackets while passing it as the last or the only argument to a function.

So, initializing a new object is the same in the following examples:

attributes = { name: 'MTF', size: 'L' }
Shirt.new(attributes)
# => <Shirt:0x005618f9ec8ae8 @attributes={:name=>"MTF", :size=>"L"}>

Shirt.new({ name: 'MTF', size: 'L' })
# => <Shirt:0x005618f9ec8ae8 @attributes={:name=>"MTF", :size=>"L"}>

Shirt.new(name: 'MTF', size: 'L')
# => <Shirt:0x005618f9ec8ae8 @attributes={:name=>"MTF", :size=>"L"}>

As you can see, Ruby returns Shirt instances that have an @attribute variable pointing to the handed hash.

At is stands, name can be used with a_shirt_object.attributes[:name] and size can be used with a_shirt_object.attributes[:size].

Thank you David for you help