DEV Community

Samuel Lubliner
Samuel Lubliner

Posted on • Edited on

Instance Methods in Ruby

Defining an Instance Method:

Instance methods are defined using the def keyword inside a class. If you prepend the class name before the method, it becomes a class-level method.

Method Return Values:

Ruby methods implicitly return the value of the last statement executed. This return value can be assigned to variables or used in further operations.

Composing Methods:

Smaller methods can be combined or used within other methods to construct more complex functionality.

Attribute Accessors:

attr_accessor creates a method to get and set a new value

Using Existing Instance Methods in New Methods

  • A method can use the results or behaviors of other methods within the same class
  • self refers to the object the method was called on
  • Used to reference other methods or attributes of that instance

Efficiently build Ruby classes with methods that interact with one another by utilizing attribute accessors, method composition, and the self keywords.

Top comments (0)