114

Is def greet; puts "hello"; end the only way to define a method on one line in Ruby?

1
  • 11
    As you can see from the answers, it's possible to define a method different ways on a single line, but the question is, should you? Any definition should be written in a way that is clear and clean for maintenance and readability reasons, so if the single-line becomes unwieldy or confusing then spread it out. Some languages seem to encourage terse coding as a way of being code-studly, but Ruby coding style encourages elegance, readability and maintainability above studliness. Accomplish the first three and we will bow to you. Commented Jan 27, 2011 at 22:37

6 Answers 6

122

You can avoid the need to use semicolons if you use parentheses:

def hello() :hello end
2
  • 1
    This is longer than using semicolons...? Commented Apr 22, 2017 at 3:52
  • @ApollyssupportsMonica if yoou read his question he already used semi-colons, so he clearly wanted an ALTERNATIVE approach :)
    – horseyguy
    Commented Mar 28, 2021 at 18:11
100

No Single-line Methods

From rubocop/ruby-style-guide#no-single-line-methods:

Avoid single-line methods. Although they are somewhat popular in the wild, there are a few peculiarities about their definition syntax that make their use undesirable. At any rate - there should be no more than one expression in a single-line method.

NOTE: Ruby 3 introduced an alternative syntax for single-line method definitions, that's discussed in the next section of the guide.

# bad
def too_much; something; something_else; end

# okish - notice that the first ; is required
def no_braces_method; body end

# okish - notice that the second ; is optional
def no_braces_method; body; end

# okish - valid syntax, but no ; makes it kind of hard to read
def some_method() body end

# good
def some_method
  body
end

One exception to the rule are empty-body methods.

# good
def no_op; end

Endless Methods

From rubocop/ruby-style-guide#endless-methods:

Only use Ruby 3.0's endless method definitions with a single line body. Ideally, such method definitions should be both simple (a single expression) and free of side effects.

NOTE: It's important to understand that this guideline doesn't contradict the previous one. We still caution against the use of single-line method definitions, but if such methods are to be used, prefer endless methods.

# bad
def fib(x) = if x < 2
  x
else
  fib(x - 1) + fib(x - 2)
end

# good
def the_answer = 42
def get_x = @x
def square(x) = x * x

# Not (so) good: has side effect
def set_x(x) = (@x = x)
def print_foo = puts("foo")

P.S.: Just to give an up-to-date full answer.

41
def add a,b; a+b end

The semicolon is the inline statement terminator for Ruby

Or you can use the define_method method. (Edit: This one's deprecated in ruby 1.9)

define_method(:add) {|a,b| a+b }
1
  • 5
    Doesn't seem to be deprecated in Ruby 2+
    – kxmh42
    Commented Feb 9, 2015 at 15:57
18

Ruby 3.0.0 adds "endless" definitions for methods with exactly one statement:

def greet = puts("hello")

Note that the one-statement limitation means that this can't be written as:

# NOT ALLOWED
def greet = puts "hello"

SyntaxError: unexpected string literal, expecting `do' or '{' or '('
def greet = puts "hello"
                 ^

It seems that this change was intended to either encourage the use of one-line methods or adjust to the reality that they are very common but hard to read -- "this kind of simple method definition [is estimated to] account for 24% of the entire method definitions" of the ruby/ruby code base.

9

Another way:

define_method(:greet) { puts 'hello' }

May be used if you don't want to enter new scope for method while defining it.

2
  • NoMethodError: private method `define_method' called for Object:Class in Ruby 1.9.3
    – Jared
    Commented Jul 25, 2012 at 16:11
  • 3
    define_method has been "privatized" in Ruby 1.9
    – edgerunner
    Commented Mar 1, 2013 at 8:46
7

Yet another way:

def greet() return 'Hello' end
2
  • 14
    In Ruby, the return value of a method is the value returned by the last statement. You don't need the return here since it's not a guard clause.
    – Damien
    Commented May 17, 2013 at 20:04
  • 1
    Upvoted because, although it is not needed, the return can add readability for those less versed in (or familiar with) Ruby. It's one of those YMMV things...
    – Potherca
    Commented Jun 15, 2020 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.