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 this video, we're going to create a class that will store our phone numbers.
Code Samples
Phone Number Class (phone_number.rb):
class PhoneNumber
attr_accessor :kind, :number
def to_s
"#{kind}: #{number}"
end
end
Contact Class
require "./phone_number"
class Contact
attr_writer :first_name, :middle_name, :last_name
attr_reader :phone_numbers
def initialize
@phone_numbers = []
end
def add_phone_number(kind, number)
phone_number = PhoneNumber.new
phone_number.kind = kind
phone_number.number = number
phone_numbers.push(phone_number)
end
def first_name
@first_name
end
def middle_name
@middle_name
end
def last_name
@last_name
end
def first_last
first_name + " " + last_name
end
def last_first
last_first = last_name
last_first += ", "
last_first += first_name
if !@middle_name.nil?
last_first += " "
last_first += middle_name.slice(0, 1)
last_first += "."
end
last_first
end
def full_name
full_name = first_name
if !@middle_name.nil?
full_name += " "
full_name += middle_name
end
full_name += ' '
full_name += last_name
full_name
end
def to_s(format = 'full_name')
case format
when 'full_name'
full_name
when 'last_first'
last_first
when 'first'
first_name
when 'last'
last_name
else
first_last
end
end
def print_phone_numbers
puts "Phone Numbers"
phone_numbers.each { |phone_number| puts phone_number }
end
end
jason = Contact.new
jason.first_name = "Jason"
jason.last_name = "Seifer"
jason.add_phone_number("Home", "123-456-7890")
jason.add_phone_number("Work", "456-789-0123")
puts jason.to_s('full_name')
jason.print_phone_numbers
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, so, we have our contact
class all set up in here.
0:00
And if we think about that, that's great.
0:04
We can have a list of contacts,
0:06
but wouldn't we also want to
enter a contact's phone number?
0:07
Yes we would.
0:13
So what we're going to do is create
a class to store phone numbers.
0:14
So hit File > New.
0:19
And we'll call this class phone_number.
0:21
And we create a phone_number.rb.
0:25
Now we can create our PhoneNumber class.
0:28
And this is gonna be
a really simple class.
0:32
We're not really gonna be working with
phone numbers too much by themselves.
0:36
So, we're just gonna make two
attribute accessors here.
0:40
The kind of phone number it is like home,
work, cell.
0:44
And, we're just going to have another
attribute called number which
0:47
stores the number.
0:51
So, we can use an attribute accessor for
that.
0:52
We'll do one for the kind and the number.
0:55
And then since we have that,
0:59
we can go ahead and
define our to_s method here.
1:02
And let's just say it's
the "#{kind}: #{number}".
1:07
Now, this is a pretty simple class so
we're not gonna make sure it works.
1:12
It's very simple so let's go ahead and now
we can add phone numbers to our contacts.
1:17
So let's go ahead and say our
contact has an attribute reader for
1:25
phone numbers because a contact will be
able to have more than one phone number.
1:34
But, this means that when we create
our contact class and initialize it,
1:39
we'll set the phone numbers
to be an empty array.
1:48
Now that we've done that, let's go ahead
and create a method to add a phone number.
1:55
To our contact.
2:00
And we'll make this method
take two arguments,
2:08
the kind and the number which
will correspond to the different
2:12
attributes that we have In
the phone_number class.
2:16
So once we do that, we'll just
create a new phone_number instance.
2:22
And we'll say the phone_number.kind
is the kind argument.
2:29
And the number is the number argument.
2:35
Once we have that we wanna append
it to the phone numbers array.
2:38
So we can say
phone_numbers.push(phone_number).
2:44
So let's go ahead and
save that and makes sure it works.
2:54
I am going to take out some of these
print statements because we don't need
3:03
to test that anymore.
3:06
So we're printing out the Jason contact.
3:10
And we'll say
jason.add_phone_number("Home",123-456-78-
3:15
90") that is my real phone number,
feel free to call me.
3:21
And let's go ahead and
add another one for work
3:31
and I'll add my real work
phone number here, okay.
3:40
Now let's go ahead and just inspect the
Jason contact and see what it looks like.
3:46
So I'm gonna run ruby contact.rb.
3:54
And you'll see we get this method.
3:58
Uninitialized constant
Contact PhoneNumber.
3:59
Now why did we get that?
4:04
Well it's because our phone number
class is in a separate file.
4:06
So when we try and
access a class that's in another file,
4:11
we need to tell Ruby that
that exists someplace else.
4:15
Or else Ruby doesn't know where it is.
4:19
We do that using the require key word.
4:21
And then we give it the path to the file.
4:28
So we're requiring, in the same directory
as this the phone number class.
4:30
I'm gonna clear my screen and
run this again.
4:37
And you'll notice,
now when we do the inspect here,
4:41
we've got this contact
with this phone numbers
4:46
array which contains two
different phone numbers.
4:51
And this is exactly what we want.
4:57
But now, let's just go ahead and
4:59
create a method to print out
the phone numbers while we are at it.
5:01
So, that we don't have
to inspect it each time.
5:04
Then we'll just call this method,
print phone numbers.
5:10
And this is going to be really easy,
since phone numbers is an array,
5:16
we can just use the each method.
5:19
And I'll say Phone Numbers right here and
then, we can just loop through them
5:21
using the each method and
print out all of the phone numbers.
5:26
Now remember,
each will take a block with one argument.
5:34
And now we can call
jason.print_phone_numbers.
5:45
Run that again.
5:56
And here we go.
5:58
Now, you might be wondering why this knows
5:58
how to print out the kind of the phone
number, and then the number, itself.
6:03
Well, it's because we are calling,
automatically,
6:07
the to_s method, which we defined
in the phone number class.
6:10
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up