Ruby adds experimental support for end-less method definition

Ruby has added a new method definition syntax as an experimental feature.

As to why:

Ruby syntax is full of “end”s. I’m paranoid that the ends end Ruby. I hope Ruby is endless. So, I’d like to propose a new method definition syntax.
- mame (Yusuke Endoh)

Syntax

def value(args) = expression

The above “endless” syntax can be used to write single-line method definitions.

Example

def say_hello() = puts("Hello World!")

say_hello() #=> "Hello World!"
def sum(x,y) = 
  x + y

sum(2,3)  #=> 5

The above examples show the most basic usage of this syntax.

Let’s see some other ways to use this new method definition syntax.

With an object

Without the endless syntax, we’d add a method to an Object instance like this:

person = Object.new

def person.speak() 
  puts("Good morning!")
end

With the new single-line method definition syntax, we can write the same method as:

person = Object.new

def person.speak() = puts("Good morning!")

person.speak() #=> Good morning!

As a private method

Without the endless syntax, we’d add a single private method to a class like this:

class User

def points_left
  is_active? @points : 0
end

private def is_active?
  @membership_end_datetime > Time.now
end

With the new single-line method definition syntax, we can write the same method as:

class User

def points_left
  is_active? @points : 0
end

private def is_active?() = @membership_end_datetime > Time.now

Limitations

There are few limitations while using this syntax:

  • Parentheses for formal arguments are mandatory while writing the definition
  • Parentheses for method call parameters (if any) in the expression are mandatory
  • The body must be an argument expression.
# Without parentheses around formal argument(s)

def square x = x**2
#=> circular argument reference - x
#=> syntax error, unexpected end-of-input, expecting ';' or '\n'


# Without parentheses around formal argument(s)

def greet = puts("Hello everyone!")
#=> syntax error, unexpected '=', expecting ';' or '\n'


# Without parentheses around a method call parameters in expression

def greet() = puts "Hello everyone!"
#=> syntax error, unexpected '=', expecting ';' or '\n'

In the snippet above, the first two examples resulted in a syntax error because parentheses for formal arguments were not used.

And the last one also resulted in a syntax error because parentheses were not used while calling the method puts.

# Using multiple lines for definition
def logout = 
  admin = Administrator.find(session[admin_id])
  admin.update(last_online_at: Time.now)
  ...

The above code will result in a NameError. Hence, multiple line definitions should always be put inside a def end block.

Summary

Ruby has added a new way to define single-line methods. However, this syntax is still in an experimenting phase, and its spec may change before the release.

More discussion about this feature can be found here.

Need help on your Ruby on Rails or React project?

Join Our Newsletter