From: "baweaver (Brandon Weaver)" Date: 2022-02-27T08:17:51+00:00 Subject: [ruby-core:107755] [Ruby master Feature#18603] Allow syntax like obj.method(arg)=value Issue #18603 has been updated by baweaver (Brandon Weaver). I believe in this particular case it would make more sense to have a dual method to `dig`, rather than adding additional complexity to the syntactic sugar around `=`. There were proposals in the past for `Enumerable#bury` that targeted this behavior: ```ruby module Enumerable def bury(*paths, &value_fn) return unless block_given? *lead, target = paths if lead.empty? # Single item in path self[target] = yield(self[target]) else above_value = self.dig(*lead) above_value[target] = yield(above_value[target]) end end end ``` The difficulty for such functions is how to differentiate between the varadic path and the value setter. For me I believe block functions strike a good medium here, and may be a viable solution, though Matz has previously rejected the idea of adding a `bury` function. Perhaps it may make sense to bring it up again if this is compelling. ---------------------------------------- Feature #18603: Allow syntax like obj.method(arg)=value https://bugs.ruby-lang.org/issues/18603#change-96680 * Author: hmdne (hmdne -) * Status: Open * Priority: Normal ---------------------------------------- I propose here to allow a syntax like: ```ruby obj.method(arg) = value ``` It would be translated to the following: ```ruby 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: ```ruby 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. -- https://bugs.ruby-lang.org/ Unsubscribe: