0.0 Introduction to Objects
Everything is an object
We will begin our journey with objects.
To make things happen using Ruby, one always puts oneself in the place of an
object and then has conversations with other objects, telling them to do stuff.
Roleplaying as an object in your program is an integral part of object-oriented programming.
To know which object you are at the moment, one may use the keyword self
.
Try it for yourself:
As you can see, if you don't specify which object you are, you automatically play the role
of the main
object that Ruby provides us by default.
We'll delve into how one can play the role of different objects and why this is useful a little further down the line.
Talking to objects
One object interacts with another by using what are called methods
. More specifically, one object "calls or invokes
the methods" of another object.
In the example below, by placing a period
(.
) after the object, then adding in the name of the
method we want to invoke.
Invoking a method on an object inevitably generates a response.
This response is always another object.
One may also chain method invocations by simply adding more periods
and method names sequentially - each method in the chain is called on
the result of the previous method.
Go on and try it by invoking next
twice on 1
to get 3
.
The results you're looking at are the consequence of running a series of tests against your input to validate it.
If you see results coloured red, this means one or more tests failed. Green means you're good to go.
Congratulations, guest!
Sign in to save your progress
or