[ruby-core:123833] [Ruby Bug#21693] Allow calling any callable object as a method
Issue #21693 has been reported by cheba (Alexander Mankuta). ---------------------------------------- Bug #21693: Allow calling any callable object as a method https://bugs.ruby-lang.org/issues/21693 * Author: cheba (Alexander Mankuta) * Status: Open * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- Callable objects are popular in Ruby. A very common pattern of Service objects with a single public `call` method can be found in many Rails apps, too. I propose to extend syntax so that adding `()` to any object that has a `call` method to work as a method invocation. Example: ```ruby class Greeter def call(name) puts "Hello, #{name}!" end end hello = Greeter.new hello("World") # => Hello, World! ``` We have a `[]` method in `Proc` that achieves similar effect but it doesn't look like a method call. There's also a dotted syntax `hello.("World")` but it also looks a little awkward and the dot is easy to forget. It's probably a bit late for Ruby 4, but I though it's a good opportunity to introduce a potentially big change. -- https://bugs.ruby-lang.org/
Issue #21693 has been updated by Eregon (Benoit Daloze). It would be a gigantic incompatibility so there is no chance for that specific syntax, as your example would call method `hello` before and no longer with your proposal. Just ```ruby p = 42 p(43) ``` would break for example with that change. ---------------------------------------- Feature #21693: Allow calling any callable object as a method https://bugs.ruby-lang.org/issues/21693#change-115249 * Author: cheba (Alexander Mankuta) * Status: Open ---------------------------------------- Callable objects are popular in Ruby. A very common pattern of Service objects with a single public `call` method can be found in many Rails apps, too. I propose to extend syntax so that adding `()` to any object that has a `call` method to work as a method invocation. Example: ```ruby class Greeter def call(name) puts "Hello, #{name}!" end end hello = Greeter.new hello("World") # => Hello, World! ``` We have a `[]` method in `Proc` that achieves similar effect but it doesn't look like a method call. There's also a dotted syntax `hello.("World")` but it also looks a little awkward and the dot is easy to forget. It's probably a bit late for Ruby 4, but I though it's a good opportunity to introduce a potentially big change. -- https://bugs.ruby-lang.org/
Issue #21693 has been updated by kddnewton (Kevin Newton). You're very close to valid Ruby with ``` class Greeter def call(name) puts "Hello, #{name}!" end end hello = Greeter.new hello.("World") ``` (Note the 1-character difference here.) Doing `hello.("World")` will automatically call the `call` method. ---------------------------------------- Feature #21693: Allow calling any callable object as a method https://bugs.ruby-lang.org/issues/21693#change-115250 * Author: cheba (Alexander Mankuta) * Status: Open ---------------------------------------- Callable objects are popular in Ruby. A very common pattern of Service objects with a single public `call` method can be found in many Rails apps, too. I propose to extend syntax so that adding `()` to any object that has a `call` method to work as a method invocation. Example: ```ruby class Greeter def call(name) puts "Hello, #{name}!" end end hello = Greeter.new hello("World") # => Hello, World! ``` We have a `[]` method in `Proc` that achieves similar effect but it doesn't look like a method call. There's also a dotted syntax `hello.("World")` but it also looks a little awkward and the dot is easy to forget. It's probably a bit late for Ruby 4, but I though it's a good opportunity to introduce a potentially big change. -- https://bugs.ruby-lang.org/
participants (3)
-
cheba (Alexander Mankuta) -
Eregon (Benoit Daloze) -
kddnewton (Kevin Newton)