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 Core and Standard Library Ruby Core Struct

Daniel Crews
Daniel Crews
14,008 Points

Struct Instantiation Not Working in Challenge

This instantiation method doesn't seem to work in the challenge:

Struct.new("Name", :first, :last)

Am I missing something?

struct.rb
Struct.new("Name", :first, :last)

1 Answer

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

this is actually a pretty tricky one, I tried the same thing at first, and it took me a few tries to understand what it was asking for here :). The key phrase in this case is "Create a Class...".

using

Struct.new("Name", :first, :last)

returns a Struct::Name:

irb(main):001:0> Struct.new("Name", :first, :last)
=> Struct::Name

so you'd end up creating instances of this with

Struct::Name.new("Nathan", "Williams")

but that's not a Name class, it's Struct::Name :/

The answer the challenge is looking for is more like

Name = Struct.new(:first, :last)

which returns an actual Name class, thereby satisfying the challenge requirements:

irb(main):002:0> Name = Struct.new(:first, :last)
=> Name

and lets you create instances via

Name.new("Nathan", "Williams")

which I think reads a little nicer, and is the recommended way to do it according to the documentation.