Syntax

Ruby Define a Method Syntax

In Ruby, define a method: defines a method — def greet(name = "friend") "Hi #{name}" end

Quick Answer

Syntax
def greet(name = "friend") "Hi #{name}" end
RubySyntax

Define a Method

Code example
def greet(name = "friend") "Hi #{name}" end

Defines a method.

Learn More

How to define a method in Ruby

  1. Open your Ruby file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Ruby Define a Method Syntax

Use def greet(name = "friend") "Hi #{name}" end.

Related Syntax References

Frequently asked questions

What is the Ruby syntax for define a method?

Use this pattern: def greet(name = "friend") "Hi #{name}" end

Can I copy this Ruby code example?

Yes. Use Copy code and adapt the example to your program.

Is this Ruby syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify