Issue #16746 has been updated by mame (Yusuke Endoh).

Assignee set to nobu (Nobuyoshi Nakada)
Status changed from Open to Assigned

Okay seriously.  You should have written many "simple" method definitions that have only one expression:

```
# three-line style
def value
  @val
end

# one-line style
def value; @val; end
```

Surprisingly, according to the following rough estimate of ruby/ruby code base, this kind of simple method definitions account for 24% of the entire method definitions.

```
# three-line style: 8570
p Dir.glob("**/*.rb").map {|f| File.binread(f).scan(/^(\s*)def.*\n.*\n\1end$/).size }.sum

# one-line style: 949
p Dir.glob("**/*.rb").map {|f| File.binread(f).scan(/^\s*def.*;[^;]*;\s*end$/).size }.sum

# all method definitions: 39502
p Dir.glob("**/*.rb").map {|f| File.binread(f).scan(/\bdef\b/).size }.sum

# propotion = (8570 + 949) / 39502 = 24%
```

I agree that `def foo(a) = expression` is best, but I cannot implement it.  Pass it to @nobu .

----------------------------------------
Feature #16746: Endless method definition
https://bugs.ruby-lang.org/issues/16746#change-84870

* Author: mame (Yusuke Endoh)
* Status: Assigned
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
----------------------------------------
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.

```ruby
def: value(args) = expression
```

As you see, there is no "end".

Examples.

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

hello("endless Ruby") #=> Hello, endless Ruby
```

```ruby
def: inc(x) = x + 1

p inc(42) #=> 43
```

```ruby
x = Object.new

def: x.foo = "FOO"

p x.foo #=> "FOO"
```

```ruby
def: fib(x) =
  x < 2 ? x : fib(x-1) + fib(x-2)

p fib(10) #=> 55
```

Limitations.

* `def: foo x = x` is invalid; the parentheses for formal arguments are mandatory.
* `private def: foo = x` is invalid; this method definition cannot be an method argument.

A patch is attached.  No conflicts.

---Files--------------------------------
endless-method-definition.patch (2.47 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>