...

/

Usage

Usage

This lesson will brief you on how to use methods in Ruby after defining them.

Once defined we can use our method

As programmers we usually say that we “call” a method. This means we ask Ruby to execute the code that the method body has, with some given arguments (input), if any.

Here’s how that looks:

Press + to interact
def add_two(number)
number + 2
end
puts add_two(3)

Explanation

Let’s inspect what’s happening here under the microscope. If you don’t understand each part of this just yet, don’t worry, we’ll get back to all of this.

On the first three lines Ruby defines the method add_two as discussed above. ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy