Defining Instance Methods
Learn to define instance methods in Ruby.
Methods can be defined and called on objects, like 1.odd?
. They can also be defined in classes, like Calculator.new
.
Methods that are available in classes are called class methods, and methods that are available on instances are called instance methods.
In this lesson, we want to add the stand-alone methods that we’ve learned about previously to our Calculator class so that they’ll end up as instance methods.
Here’s how we can do that:
Press + to interact
class Calculatordef sum(number, other)number + otherendend
We simply move the method into the class body so that it’s enclosed by it.
...