loaf_of_bread.sliceHere, the slice method is called on a loaf_of_bread, and the start method is run on my_car. Inside the object definitions for loaf_of_bread and my_car, there are method definitions (like functions, but related to a specific object) called slice and start.
my_car.start
Methods can be chained together simply by adding them to the call.
loaf_of_bread.slice.butterYou can also pass arguments into a specific method.
my_car.start.drive
my_car.start.drive('home')Methods are typically defined in and for each individual object, but there is a special object called the Kernel in Ruby that all other objects inherit from. Kernel methods are accessed from anywhere in your Ruby scripts, and do not require the object name - if you've worked with even a small amount of Ruby, you've already seen several Kernel methods like print, chomp, and even your Ruby data types like Array and String.
- Everything in Ruby is an object, and methods are actions on those objects.
- Methods can be chained.
- Methods can be given arguments.
- A Kernel method is available anywhere in your script.
