Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In Ruby, a Struct is a shortcut to creating a class with accessor methods. You could create these yourself using simple classes but structs save you a bit of time.
Links
Code Samples
Here is our struct:
Customer = Struct.new(:name, :email) do
def name_with_email
"#{name} <#{email}>"
end
end
We can then create a new instance and iterate through the attributes:
customer = Customer.new("Jason", "jason@teamtreehouse.com")
customer.each_pair do |name, value|
puts "#{name} - #{value}"
end
Now we're going to take a look at structs.
0:00
In Ruby, a struct is a shortcut to
creating a class with accessor methods.
0:03
You could create these yourself
using simple classes, but
0:10
structs save you a bit of time.
0:13
Let's see how structs work
now using workspaces.
0:16
So here is the struct documentation.
0:20
Now if you look at
the notes below the video
0:23
you will be able to see the struct
documentation in there.
0:26
So let's go ahead and
see what a struct is.
0:29
If we scroll down here we can
see that we can create a struct
0:32
by calling new, passing in a string for
0:37
the class that we want, and
then the attributes that are present.
0:40
We can also use the class
name that we want with and
0:46
equals sign and
then Struct.new with the attributes.
0:49
Let's go ahead and see how that works.
0:55
So over here,
I've launched a new Ruby workspace and
0:57
right now, I have a class called customer,
the customer has two attributes.
1:00
A name and an email.
1:05
Now this is something we've seen before.
1:07
This is just a standard class, and
then we instantiate a new customer and
1:09
inspect it, and I'm just gonna
show you how this works, and
1:14
here we see this customer
has been instantiated.
1:19
So, if we wanted to do this as a struct
we could take all this out here.
1:23
And just say customer is a new
struct with a name and email.
1:37
Now I'm gonna clear
the screen down here and
1:44
run this again, and if we look,
we see that we have the exact same thing.
1:48
The only difference is the class has
changed from customer to a struct,
1:53
and it is still a class,
just a struct customer class.
1:58
Now if we wanted to,
we could add in a block here.
2:05
Then anything inside this block would be
2:10
evaluated in the context
of this struct class.
2:13
So we can write instance
methods here as well.
2:17
As an example,
we could say name with email,
2:21
and then evaluate the name,
and the email address.
2:27
And then I'm just gonna print that out
to the screen, customer.name_with_email.
2:33
Now if I were to run this again,
we can see that the name and
2:41
email address are printed out,
just gonna clear that.
2:45
Structs are neat because
each of the attributes
2:50
will be passed as a block
to the each method.
2:53
So I'm gonna clear my screen,
run this again and
3:07
we can see the attribute was printed
to the screen the value of it.
3:10
If we look back at
the struct documentation,
3:15
and scroll down here we can
see there's also each pair.
3:19
That we can pass in so
we get the attribute name and the value.
3:24
So let's go ahead and change that.
3:27
Name and value, and
we'll just say, name and value.
3:35
So I'm gonna save that, run it again, and
3:41
we can see the name and
value are printed out for each attribute.
3:46
Now when you're creating a struct if you
start having a lot of methods in here,
3:50
it might be time to change and
create a real class out of it.
3:56
Structs are mainly meant to be
convenience classes when creating
4:01
something with just a couple of attributes
that you want to access in this format.
4:06
Try creating some struct on
your own now using workspaces.
4:11
You need to sign up for Treehouse in order to download course files.
Sign up