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 trialsir a
Courses Plus Student 3,692 Pointswhere 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
11,051 PointsIn 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]
.
sir a
Courses Plus Student 3,692 Pointssir a
Courses Plus Student 3,692 PointsThank you David for you help