Consider our earlier example of the "dot operator" applied to some text:
irb(main)> "hello".capitalize
=> "Hello"
The period character, commonly known as the "dot", combines an object with a method.
In the previous example, the capitalize
method performs work upon
the String object "hello"
.
The result of this work — the string "Hello"
— is the result of evaluating the entire expression "hello".capitalize
.
All objects (that is, variables or literal values) can have methods applied to them by using the dot operator. However, not all methods are applicable to all objects.
Consider the following:
irb(main)> "abc".upcase
=> "ABC"
irb(main)> 7.upcase
NoMethodError: undefined method `upcase' for 7:Integer
It doesn't make sense to apply the upcase
method to the
number 7
, so Ruby reports that there's no such method
by that name that can be applied to the 7
which is an Integer
.
But here is something we can do with numbers that we can't do with text:
irb(main)> 7.odd?
=> true
irb(main)> 7.even?
=> false
Cool, no?
This all leads us to the following golden rule:
The class of an object determines that methods that can be applied to it.