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