HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RubyVariablesData TypesOperatorsStringsArraysHash (Dictionary)Conditions (if / else)LoopsMethods (def)Classes
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Hash (Dictionary)
🔁 Condition & Loop
🧩 Method & Class
Ruby›Methods (def)

Methods (def)

Defining functions with def, parameters, automatic return, and default values.

Simple enough for a 5-year-old to understand

Method with def

Ruby automatically returns the value of the last expression — no need to write return.

Example code
1
def square(x)
Method taking x.
FunctionMethod taking x.
2
x * x
Last expression returns automatically.
FunctionLast expression returns automatically.
3
end
Close.
FunctionClose.
4
puts square(5)
25
Output / Input25
FunctionOutput / Input
💡 Hint: return is optional; the value of the last line is returned anyway.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Write a method named addUp that adds two numbers; print addUp(8, 9).

🎯 Expected output: 17

Loading editor…

About this topic

In Ruby, methods are defined with def and you don't need to write return for the return value: the value of the last expression is returned automatically. return is only used when you need to exit a method early.

There are two meaningful conventions in Ruby method naming. Methods ending with a question mark return true or false; those ending with an exclamation mark modify the object in place or carry unexpected behavior. These are conventions, not rules, but they are universally applied.

Ruby is flexible with method parameters: you can provide default values, use keyword parameters, and accept a variable number of arguments. Keyword parameters make what a call means readable in methods with many parameters.

What you will learn

  • defining methods with def
  • implicit return value
  • default and keyword parameters
  • the question and exclamation mark naming convention
  • writing methods that accept blocks

When to use?

If there are three or more parameters, use keyword parameters: the person reading the call sees what each value is and the mistake of mixing up the order is eliminated.

If the same code is repeated in two places, it gets moved to a method; the fix is made in one place and all call sites are fixed.

Common mistakes

Wrong

def greet(name)
  puts "Hello #{name}"
end

message = greet("Ali")
puts message.upcase

puts always returns nil, not the text it printed. message becomes nil and calling a method on it results in the "undefined method 'upcase' for nil" error. If a method wants to return a value, it should return it instead of printing.

Correct

def greet(name)
  "Hello #{name}"
end

message = greet("Ali")
puts message.upcase

Wrong

def add(a, b = 0, c)
  a + b + c
end

puts add(1, 2)

A parameter without a default cannot come after a parameter with a default; Ruby gives a syntax error. Since missing arguments are filled from the right, defaults must be at the end of the list. Using keyword parameters removes this restriction.

Correct

def add(a, c, b = 0)
  a + b + c
end

puts add(1, 2)

Wrong

def find_user(id)
  return nil if id.nil?
  { id: id, name: "Ali" }
end

user = find_user(nil)
puts user[:name]

The method can return nil but the caller doesn't check; calling square brackets on nil results in the "undefined method for nil" error. Methods returning nil must be checked at the call site; alternatively, raising an error shows the problem at its source.

Correct

def find_user(id)
  return nil if id.nil?
  { id: id, name: "Ali" }
end

user = find_user(nil)
if user
  puts user[:name]
else
  puts "User not found"
end

Frequently asked questions

should I write return?

Not necessary; the value of the last expression returns automatically and this is the idiomatic way in Ruby. return is only used when you need to exit a method early — such as returning immediately when a condition is not met.

What do method names ending with a question mark mean?

That they return true or false. This is a convention, not a rule, but it is universally applied in Ruby and makes the code readable. You are expected to follow this convention in your own methods as well.

Can a method return multiple values?

Yes, by separating them with commas; Ruby returns these as an array, and you can unpack them into separate variables on the calling side. If the values form a meaningful whole, returning a hash is more readable because each value gets a name.

Summary

The value of the last expression returns automatically; return is not needed.

puts returns nil; a method that wants to return a value should return it, not print it.

Parameters with default values must be placed at the end of the list.

The question mark indicates a boolean return, the exclamation mark indicates in-place modification.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code