0
0
Rubyprogramming~10 mins

Implicit return (last expression) in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Implicit return (last expression)
📖 Scenario: Imagine you are creating a simple calculator function in Ruby that adds two numbers. Ruby automatically returns the value of the last expression in a method without needing an explicit return statement.
🎯 Goal: Build a Ruby method that adds two numbers and returns the result using implicit return (last expression).
📋 What You'll Learn
Create a method named add_numbers that takes two parameters: a and b.
Inside the method, add a and b without using the return keyword.
Call the add_numbers method with the numbers 5 and 7.
Print the result of the method call.
💡 Why This Matters
🌍 Real World
Implicit return makes Ruby code shorter and easier to read, especially for simple calculations or data processing.
💼 Career
Understanding implicit return helps you write clean Ruby methods, a skill useful in web development with Ruby on Rails and scripting.
Progress0 / 4 steps
1
Create the add_numbers method with parameters
Write a method named add_numbers that takes two parameters called a and b.
Ruby
Need a hint?

Use def to start a method and end to finish it.

2
Add a and b inside the method without return
Inside the add_numbers method, write an expression that adds a and b without using the return keyword.
Ruby
Need a hint?

Just write a + b as the last line inside the method.

3
Call the add_numbers method with 5 and 7
Call the add_numbers method with arguments 5 and 7 and save the result in a variable named result.
Ruby
Need a hint?

Use result = add_numbers(5, 7) to call the method and store the answer.

4
Print the result
Write a puts statement to print the value stored in result.
Ruby
Need a hint?

Use puts result to show the answer on the screen.