Project

General

Profile

Actions

Feature #21693

open

Allow calling any callable object as a method

Feature #21693: Allow calling any callable object as a method

Added by cheba (Alexander Mankuta) about 6 hours ago. Updated about 1 hour ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:123833]

Description

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:

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.

Updated by cheba (Alexander Mankuta) about 1 hour ago Actions #1

  • Tracker changed from Bug to Feature
  • Backport deleted (3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN)
Actions

Also available in: PDF Atom