Project

General

Profile

Actions

Feature #18603

open

Allow syntax like obj.method(arg)=value

Added by hmdne (hmdne -) 5 months ago. Updated 5 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:107751]

Description

I propose here to allow a syntax like:

obj.method(arg) = value

It would be translated to the following:

obj.__send__(:method=, arg, value)

The lack of this syntax kind of limits the ability to design DSLs in Ruby in my opinion. I don't think this would bring any conflicts with existing parser rules.

My proposal would be to put the value at the last argument, akin to how []= works. So, for example this code would work:

module Indexable
  def dig=(*path, last, value)
    if path.empty?
      self[last] = value
    else
      first = path.shift
      self[first]&.dig(*path, last) = value
    end
  end
end

Hash.include Indexable
Array.include Indexable

The kwargs may be supported similarly to how they work on []=, ie. becoming a penultimate Hash argument. While maybe not perfect, it is consistent with how []= works and I imagine most usecases won't require kwargs.

Actions

Also available in: Atom PDF