DEV Community

loading...

Endless Ruby

Anubhav Jain
I'm a passionate Rubyist, learning about how Ruby runs internally.
・1 min read

A very interesting feature was proposed to be added to Ruby. If you can imagine from the title - Yes! endless Ruby. But in what context? Let's see in this blog post.

Method definition in Ruby is done this way :

def some_method
# one-line method body
end 
Enter fullscreen mode Exit fullscreen mode

The proposal is to allow the below syntax for method definitions

def value(args) = expression
Enter fullscreen mode Exit fullscreen mode

You see, there is no end keyword hence endless.
This just applies to method definitions.

With this in place, the below examples can be considered

def hello(name) = 
  puts "Hello #{name}!"

hello('endless Ruby') #=> Hello endless Ruby!
Enter fullscreen mode Exit fullscreen mode

and

def square(x) = x * x

p square(4) #=> 16
Enter fullscreen mode Exit fullscreen mode

and

x = Object.new
def x.foo = "FOO"

p x.foo #=> "FOO"
Enter fullscreen mode Exit fullscreen mode

I felt it would a nice thing to add, as we often write one-line methods.

Discussions on this proposal are happening here. Feel free to visit and express your thoughts.

Discussion (0)