The caller is the method or function that invokes another method at a call site. It’s one half of the caller/callee relationship fundamental to program execution.
What is a Caller?
def caller_method
callee_method() # ← caller_method is the caller
end
def callee_method
puts "called!"
endThe caller is the active method that initiates a call to another method.
The Call Relationship
Every method call establishes a caller/callee pair:
def a
b() # 'a' is caller, 'b' is callee
end
def b
c() # 'b' is caller, 'c' is callee
end
def c
# 'c' has no calls, so it's never a caller in this chain
endA method can be both caller (when it calls others) and callee (when it’s called).
Caller in the Call Stack
The caller appears below the callee in the call stack:
Call Stack (grows downward):
┌──────────────┐
│ main │ ← Original caller
├──────────────┤
│ a │ ← Caller of 'b'
├──────────────┤
│ b │ ← Caller of 'c', callee of 'a'
├──────────────┤
│ c │ ← Current executing, callee of 'b'
└──────────────┘
Caller Context in YARV
In YARV, the caller’s frame is saved when making a call:
def caller_method
x = 10
callee_method() # ← Caller frame saved
x + 1 # ← Execution resumes here after callee returns
endThe caller’s frame includes:
- Program counter pointing to the instruction after the call
- Local variables and state
- Stack pointer position
When the callee returns, the caller’s frame is restored and execution continues.
Accessing Caller Information
Ruby provides ways to inspect the caller:
def callee_method
puts caller # Shows backtrace of calling methods
puts caller_locations.first.label # Shows caller method name
end
def caller_method
callee_method
end
caller_method
# Output:
# caller_method (from caller)
# <main> (from caller_locations)This is useful for debugging and understanding call flow.
Caller-Saved State
When a call site executes, the caller must save certain state:
Caller-saved registers (in native code):
Before calling:
Save: temporary values, return address
Call: jump to callee
After return:
Restore: saved values
Continue: with result
In YARV, this happens automatically through frame management.
Caller and Return Values
The caller receives the callee’s return value:
def caller_method
result = callee_method() # Caller receives return value
puts "Got: #{result}"
end
def callee_method
42 # Return value
endIn stack-based VMs, the return value is left on the stack for the caller to use.