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