Everything in the world of Ruby is an object. An object is a box of information (data) and actions (methods). If we treated a cat like an object, it would have data like a height or attitude or age. It would also have behaviors like purring, scratching, and sleeping. Because all cats share similar actions and kinds of information, we can put them in an even bigger box called Cat.
Cat with a capital "C" is a class, and it's an object with the types of information and actions that can be found in every cat object inside it. Since it holds the things that are in common across all cats, each cat can just think about the things it knows and does special.
Say we have a cat object called tom. If we want tom to do something, we have to send tom a message. We can send tom a message in Ruby by using a . and the name of the thing we want tom to do. When we write tom.chase in Ruby, we are writing a message in three parts. tom gets the message, . means we're sending a message, and chase is the method we're sending. tom.chase means, "tom, please chase"
tom is an object, a box of info and actions. When tom gets the chase message, he'll look inside his box for how to chase. If he can't find any instructions labeled chase, he will look outside his box in the big Cat box to see if there are any instructions labeled chase there.
Since everything in Ruby is an object, you better believe that Cat is not the biggest box. Cat might be inside another box called Animal. If tom doesn't find any instructions for how to chase in Cat, he'll check the giant Animal box. He will keep looking in bigger and bigger boxes until he either finds some instructions labelled chase or he gets to the box the size of the Ruby universe—the BasicObject box.
If tom can't find any instructions in the BasicObject box, a cry echoes through the Ruby universe, an error is raised, time stops, and the Ruby universe collapses until it's created again.
Ruby on Rails developer - Maker of ✨ things on the Internet. O(🐌^n) kind of guy. Alumni @lewagonparis (batch 145). Builds wooden furniture on his balcony.
Everything in the world of Ruby is an object. An object is a box of information (data) and actions (methods). If we treated a cat like an object, it would have data like a height or attitude or age. It would also have behaviors like purring, scratching, and sleeping. Because all cats share similar actions and kinds of information, we can put them in an even bigger box called
Cat
.Cat
with a capital "C" is a class, and it's an object with the types of information and actions that can be found in everycat
object inside it. Since it holds the things that are in common across allcat
s, eachcat
can just think about the things it knows and does special.Say we have a cat object called
tom
. If we wanttom
to do something, we have to sendtom
a message. We can sendtom
a message in Ruby by using a.
and the name of the thing we wanttom
to do. When we writetom.chase
in Ruby, we are writing a message in three parts.tom
gets the message,.
means we're sending a message, andchase
is the method we're sending.tom.chase
means, "tom
, pleasechase
"tom
is an object, a box of info and actions. Whentom
gets thechase
message, he'll look inside his box for how tochase
. If he can't find any instructions labeledchase
, he will look outside his box in the bigCat
box to see if there are any instructions labeledchase
there.Since everything in Ruby is an object, you better believe that
Cat
is not the biggest box.Cat
might be inside another box calledAnimal
. Iftom
doesn't find any instructions for how tochase
inCat
, he'll check the giantAnimal
box. He will keep looking in bigger and bigger boxes until he either finds some instructions labelledchase
or he gets to the box the size of the Ruby universe—theBasicObject
box.If
tom
can't find any instructions in theBasicObject
box, a cry echoes through the Ruby universe, an error is raised, time stops, and the Ruby universe collapses until it's created again.There's also a great StackOverflow thread on the Ruby method lookup path here.
This is crystal clear @levi ! Thank you so much for this. Love the Russian dolls-y metaphor here. 👏