Method Arguments

Ruby methods accept arguments in several styles — positional, keyword, splat, double-splat, and block — and choosing the right mix makes your methods both flexible and readable.

Learn Method Arguments in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll combine all five argument types in one signature and forward them effortlessly with the (...) shorthand.

What You'll Learn in This Lesson

1️⃣ Positional vs Keyword Arguments

Positional arguments are bound by order; keyword arguments (a name with a trailing colon) are bound by name and can appear in any order. Add a default after the colon — greeting: "Hello" — to make a keyword optional. A keyword with a bare colon and no default is required .

2️⃣ Splat (*args) and Double Splat (**kwargs)

A single splat *args gathers any number of extra positional arguments into an Array; a double splat **kwargs gathers extra keyword arguments into a Hash. You can combine them with required and keyword parameters in a single signature — Ruby sorts each argument into the right bucket.

3️⃣ Block Capture (&block) and Forwarding (...)

Add &block as the last parameter to capture a block as a callable Proc you can store or pass on. And when you just want to wrap another method, the (...) shorthand forwards every positional, keyword, and block argument in one stroke.

🎯 Your Turn

Make the role keyword optional by supplying a default. Replace the ___ so a call without role: falls back to "member" .

Write one method that uses a required positional, a splat for path segments, an optional scheme: keyword, and a double splat for query parameters. Run with ruby url.rb .

📋 Quick Reference — Method Arguments

Practice quiz

How are positional arguments matched to parameters?

  • by name
  • alphabetically
  • by order
  • by type

Answer: by order. Positional arguments bind to parameters by their position.

What syntax marks a keyword argument in a method definition?

  • a trailing colon, like name:
  • a leading colon
  • an asterisk
  • an ampersand

Answer: a trailing colon, like name:. A name followed by a colon, def f(name:), declares a keyword argument.

A keyword argument written name: with no default value is:

  • optional
  • ignored
  • a syntax error
  • required

Answer: required. A bare keyword with no default must be supplied by the caller.

How do you make a keyword argument optional?

  • wrap it in parentheses
  • give it a default, like role: "member"
  • put it last
  • prefix it with *

Answer: give it a default, like role: "member". Supplying a default value makes the keyword optional.

What does *args (single splat) collect?

  • extra positional arguments into an Array
  • extra keyword arguments into a Hash
  • a block
  • nothing

Answer: extra positional arguments into an Array. A single splat gathers leftover positional arguments into an Array.

What does **kwargs (double splat) collect?

  • positionals into an Array
  • a block as a Proc
  • extra keyword arguments into a Hash
  • the method name

Answer: extra keyword arguments into a Hash. A double splat gathers leftover keyword arguments into a Hash.

What does &block capture in a method's parameter list?

  • the return value
  • a block as a callable Proc
  • all keywords
  • the class

Answer: a block as a callable Proc. &block captures a passed block as a Proc you can store or call.

What does the (...) shorthand do when forwarding to another method?

  • forwards nothing
  • forwards only positionals
  • forwards only the block
  • forwards every positional, keyword, and block argument

Answer: forwards every positional, keyword, and block argument. (...) forwards all arguments and the block in one stroke.

In Ruby 3, how do you splat a hash into keyword arguments?

  • pass it as the last positional
  • pass it explicitly as **my_hash
  • Ruby converts it automatically
  • you cannot

Answer: pass it explicitly as **my_hash. Ruby 3 separated keywords from hashes, so you must use **my_hash.

Which parameter order is valid in a method signature?

  • **opts before *args
  • &block first
  • required, *splat, keywords, **double-splat, &block
  • keywords before required positionals only

Answer: required, *splat, keywords, **double-splat, &block. Positionals/splat come first, then keywords/double-splat, then &block.