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
An object's "class" decides what methods are available on it.
- Can get a list of the methods available on an object with the
methods
method:p 2.methods.sort
,p "AA".methods.sort
$ irb
2.3.0 :001 > 2.methods.sort
=> [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, :<=>, :==, :===, :=~, :>, :>=, :>>, :[], :^, :__id__, :__send__, :abs, :abs2, :angle, :arg, :between?, :bit_length, :ceil, :chr, :class, :clone, :coerce, :conj, :conjugate, :define_singleton_method, :denominator, :display, :div, :divmod, :downto, :dup, :enum_for, :eql?, :equal?, :even?, :extend, :fdiv, :floor, :freeze, :frozen?, :gcd, :gcdlcm, :hash, :i, :imag, :imaginary, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :integer?, :interesting_methods, :is_a?, :itself, :kind_of?, :lcm, :magnitude, :method, :methods, :modulo, :negative?, :next, :nil?, :nonzero?, :numerator, :object_id, :odd?, :ord, :phase, :polar, :positive?, :pred, :private_methods, :protected_methods, :public_method, :public_methods, :public_send, :quo, :rationalize, :real, :real?, :rect, :rectangular, :remainder, :remove_instance_variable, :respond_to?, :round, :send, :singleton_class, :singleton_method, :singleton_method_added, :singleton_methods, :size, :step, :succ, :taint, :tainted?, :tap, :times, :to_c, :to_enum, :to_f, :to_i, :to_int, :to_r, :to_s, :truncate, :trust, :untaint, :untrust, :untrusted?, :upto, :zero?, :|, :~]
2.3.0 :002 > "AA".methods.sort
=> [:!, :!=, :!~, :%, :*, :+, :+@, :-@, :<, :<<, :<=, :<=>, :==, :===, :=~, :>, :>=, :[], :[]=, :__id__, :__send__, :ascii_only?, :b, :between?, :bytes, :bytesize, :byteslice, :capitalize, :capitalize!, :casecmp, :center, :chars, :chomp, :chomp!, :chop, :chop!, :chr, :class, :clear, :clone, :codepoints, :concat, :count, :crypt, :define_singleton_method, :delete, :delete!, :display, :downcase, :downcase!, :dump, :dup, :each_byte, :each_char, :each_codepoint, :each_line, :empty?, :encode, :encode!, :encoding, :end_with?, :enum_for, :eql?, :equal?, :extend, :force_encoding, :freeze, :frozen?, :getbyte, :gsub, :gsub!, :hash, :hex, :include?, :index, :insert, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :interesting_methods, :intern, :is_a?, :itself, :kind_of?, :length, :lines, :ljust, :lstrip, :lstrip!, :match, :method, :methods, :next, :next!, :nil?, :object_id, :oct, :ord, :partition, :prepend, :private_methods, :protected_methods, :public_method, :public_methods, :public_send, :remove_instance_variable, :replace, :respond_to?, :reverse, :reverse!, :rindex, :rjust, :rpartition, :rstrip, :rstrip!, :scan, :scrub, :scrub!, :send, :setbyte, :singleton_class, :singleton_method, :singleton_methods, :size, :slice, :slice!, :split, :squeeze, :squeeze!, :start_with?, :strip, :strip!, :sub, :sub!, :succ, :succ!, :sum, :swapcase, :swapcase!, :taint, :tainted?, :tap, :to_c, :to_enum, :to_f, :to_i, :to_r, :to_s, :to_str, :to_sym, :tr, :tr!, :tr_s, :tr_s!, :trust, :unicode_normalize, :unicode_normalize!, :unicode_normalized?, :unpack, :untaint, :untrust, :untrusted?, :upcase, :upcase!, :upto, :valid_encoding?]
- The object's class decides which methods are available on it.
- You can call the
class
method on any object to find out what its class is.
2.3.0 :003 > 2.class
=> Fixnum
2.3.0 :004 > "AA".class
=> String
- You already know how to create objects from the
String
andFixnum
classes.- Just include text between quotation marks in your code, and you'll have a
String
- Likewise, just include a whole number in your code, and you'll have a
Fixnum
- Just include text between quotation marks in your code, and you'll have a
2.3.0 :003 > 2.class
=> Fixnum
2.3.0 :004 > "AA".class
=> String
For other classes, you usually have to call a method on the class to get an instance of it. For example, earlier we briefly showed how to get an instance of the Time
class, which represents the current time, by calling the Time.now
method.
2.3.0 :005 > time = Time.now
=> 2017-09-02 23:11:23 -0700
2.3.0 :006 > time.class
=> Time
Here are links to documentation on some Ruby classes and their methods:
We can call a Length
method on a string and
0:00
we can call an even method on an integer.
0:02
One thing we can't do is call
the length method on an integer.
0:05
Let's save that and try running it.
0:10
We get the error undefined
method length for Fixnum.
0:17
We also can't call the even
method on a string.
0:22
Save that and try running it.
0:27
This time we get the error
undefined method even for a String.
0:31
Not only did those
operations not make sense,
0:35
the methods don't exist on those objects.
0:38
A string has a length method but
not an even method and
0:40
an integer has an even method but
not a length method.
0:44
We can get a list of the methods available
on an object by calling a method named
0:47
methods on it.
0:52
so let's try that now
with an integer object.
0:54
We'll call pita printout result and
we'll say 2.methods and
0:57
we'll also chain a call to a method
named sort on the result, so
1:02
that the methods are sorted
in alphabetical order.
1:05
Let's try running that.
1:08
And you can see,
a large list of methods get returned.
1:11
Let's do the same with the string.
1:16
We'll call p AA .methods.sort,
save that, try running it.
1:18
And again, you can see the large
list of methods get returned but
1:28
they're different methods this time.
1:31
So what decides which methods
are available on an object?
1:33
It's the object's class.
1:37
You can call the class method on any
object to find out what it's class is.
1:39
So let's try printing AA.class and
let's try printing 2.class.
1:43
And if we run that, we see that
we get String for the string and
1:53
a class called Fixnum for the integer.
1:57
You can think of a class as a blueprint.
2:00
It's like a set of plans for building say,
a car or a radio, or a house.
2:02
Objects are constructed
using the blueprint.
2:07
The blueprint says how the objects
should be structured and
2:10
what they should be able to do.
2:14
But the blueprint doesn't get into
specific details about an individual car,
2:15
radio or house, like their color.
2:19
Likewise, a Ruby class
specifies a new type of data.
2:22
Objects are then constructed using that
class, we call these instances of a class.
2:26
The class specifies how
the data should be stored and
2:32
the things you should be
able to do using that data.
2:35
A class doesn't specify anything about
its individual instances though,
2:37
like the data they hold,
that's left up to the objects themselves.
2:41
You already know how to create objects
from the string and fixed num classes.
2:46
Just include text between quotation marks
in your code and you'll have a string.
2:50
Likewise, just include a whole number in
your code and you'll have a fixed num.
2:55
For other classes,
2:59
you usually have to call a method on
the class to get an instance of it.
3:00
For example, earlier we briefly showed how
to get an instance of the time class which
3:03
represents the current time by
calling the Time.now method.
3:08
We can assign that new time object to
a variable by saying time equals Time.now,
3:13
we can name the variable whatever we want,
of course.
3:18
And if we print out its class by
calling the class method on it.
3:21
Let's try running that, we'll see that the
result is an instance of the Time class.
3:29
Now as I mentioned, you can get
a list of the names of methods on
3:36
an object by calling its methods, method.
3:39
So if we create a string and call methods
on it and then print the result out.
3:43
We'll see a big list of methods available
on it but that doesn't tell you what
3:52
the methods do, what arguments they take,
what their return values are, or anything.
3:56
To learn all that, you'll need to
look at the classes documentation.
4:00
There's lots of great Ruby
documentation sites out on the web.
4:04
We just need to look them up.
4:07
First, we need to be sure what class
we're looking for documentation for.
4:09
You can confirm that by
calling the class method
4:13
on the object that you want to look at.
4:16
So, of course, this is a string.
4:18
The class method returns string for
this object.
4:20
So, I know I need documentation for
the string class.
4:23
The next step is to look for
that class in a web search engine.
4:26
But I can't just type string
into the search engine,
4:31
that'll bring up a lot of results for
similar classes in other languages.
4:33
Instead I need to type ruby string.
4:38
That will limit the results to pages
that also contain the word ruby.
4:43
The top result is for Class String.
4:48
That looks promising, so I'll click on it.
4:50
The methods available on a string object
are listed down the side of the page.
4:53
Each is a link to documentation for
that method within the page.
4:57
Let's click on reverse.
5:03
It looks like it returns a new copy of
the string but turned around backwards.
5:05
That sounds like fun, so let's try it.
5:09
We'll go back to our program.
5:11
And we'll create a string object and
call reverse on it.
5:15
And that returns a string, so we'll need
to call puts to print the result out.
5:20
Let's try running this.
5:24
And we get the string olleh or
hello reversed.
5:27
Let's see what else is
in the documentation,
5:31
Here's the length method that we were
calling earlier, it says that it returns
5:36
the length of a string in
characters which we know is true.
5:40
And here, near at the top of the list is
a chomp method, let's see what that does.
5:45
It looks like it returns
a new copy of the string but
5:50
with new line characters removed.
5:53
That should help us with our widget store
program because we still need to remove
5:55
the new line character at the end
of the user's keyboard input.
5:59
You need to sign up for Treehouse in order to download course files.
Sign up