Skip to content

Endless method definition [Feature #16746] - #2996

Merged
matzbot merged 1 commit into
ruby:masterfrom
nobu:feature/16746-endless-def
Apr 10, 2020
Merged

Endless method definition [Feature #16746]#2996
matzbot merged 1 commit into
ruby:masterfrom
nobu:feature/16746-endless-def

Conversation

@nobu

@nobu nobu commented Apr 2, 2020

Copy link
Copy Markdown
Member

Synopsis

The new method definition syntax proposed by @mame at Feature #16746, then noted by @matz.

def value(args) = expression

As you see, there is no "end".

Examples

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

hello("endless Ruby") #=> Hello, endless Ruby
def inc(x) = x + 1

p inc(42) #=> 43
x = Object.new

def x.foo = "FOO"

p x.foo #=> "FOO"
def fib(x) =
  x < 2 ? x : fib(x-1) + fib(x-2)

p fib(10) #=> 55

@krzysiek1507

Copy link
Copy Markdown

Only a one line definition is possible?

@nobu

nobu commented Apr 2, 2020

Copy link
Copy Markdown
Member Author

Newlines are not significant.

@sevk

sevk commented Apr 3, 2020

Copy link
Copy Markdown

keep syntax simple . compatible . easy to learn .

@nobu
nobu force-pushed the feature/16746-endless-def branch from 66a1b65 to 5f1f0e2 Compare April 3, 2020 09:22
@krzysiek1507

krzysiek1507 commented Apr 3, 2020

Copy link
Copy Markdown

So this also works?

def notify(user_id) =
  user = User.find user_id
  Mailer.notify user
  DoSomethingWorker.perform user

p notify(10)

@nobu

nobu commented Apr 3, 2020

Copy link
Copy Markdown
Member Author

No, the body must be an argument expression.

@krzysiek1507

Copy link
Copy Markdown

Thanks!

@nobu

nobu commented Apr 5, 2020

Copy link
Copy Markdown
Member Author

Here is just a patch.
As for the spec, please join Feature #16746.

@havenwood

Copy link
Copy Markdown
Contributor

As you see, there is no "end".

copying syntax to reduce code readability. there is an inevitable end, please stop this.

I think delimiting that a method is no more than an argument expression actually adds to readability. It's valuable to be able to quickly ascertain when a thing is just the simple variant. As @mame points out, 24% of methods in the Ruby codebase are just an argument expression. It's just a tiny bit of syntax to learn, which seems worth it to eek out a little more quick comprehensibility for this very common case of simple method definition.

@hsbt

hsbt commented Apr 5, 2020

Copy link
Copy Markdown
Member

Do not discuss spec in this PR.

@ruby ruby locked and limited conversation to collaborators Apr 5, 2020
@nobu
nobu force-pushed the feature/16746-endless-def branch from 5f1f0e2 to a948d0b Compare April 7, 2020 15:28
@nobu
nobu force-pushed the feature/16746-endless-def branch from a948d0b to e8f5369 Compare April 10, 2020 09:05
@matzbot
matzbot merged commit e8f5369 into ruby:master Apr 10, 2020
@nobu
nobu deleted the feature/16746-endless-def branch April 10, 2020 10:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants