Feature #6841

Shorthand for Assigning Return Value of Method to Self

Added by Tom Wardrop over 1 year ago. Updated about 1 year ago.

[ruby-core:47036]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:Next Major

Description

=begin
Quite often in Ruby, I find myself doing something like: (({myvar[:foo][:bar] = myvar[:foo][:bar].toi})) or (({obj.foo.bar = obj.foo.bar.toi})). Realising this, I thought of what would be a fairly nice shorthand syntax for this, which could be: (({myvar[:foo][:bar] .= toi})). How this works should be pretty self-explanatory. The (({.=})) operator works exactly like any other assignment operator of this nature.

Would be nice to see this in Ruby 2.0. Wondering what others think of this?
=end

History

#1 Updated by Tom Wardrop over 1 year ago

=begin
The formatting obviously didn't work. Try this one:

Quite often in Ruby, I find myself doing something like: (({myvar[:foo][:bar] = myvar[:foo][:bar].toi})) or (({obj.foo.bar = obj.foo.bar.toi})). Realising this, I thought of what would be a fairly nice shorthand syntax for this, which could be: (({myvar[:foo][:bar] .= toi})). How this works should be pretty self-explanatory. The (({.=})) operator works exactly like any other assignment operator of this nature.

Would be nice to see this in Ruby 2.0. Wondering what others think of this?
=end

#2 Updated by Nobuyoshi Nakada over 1 year ago

  • Description updated (diff)

#3 Updated by Thomas Sawyer over 1 year ago

=begin
Does . effectively become a "call dispatch operator" then? Could one write:

a.send('.', :foo)

I like the idea. But to complete the comparison to other operators, it made me think . would be some sort of method.
=end

#4 Updated by Tom Wardrop over 1 year ago

Yeah, I thought about making "." a method, but I don't think that would be advantageous. If you were to overload ".", perhaps to intercept all method calls (can't think of any other reasons besides as hook method), it wouldn't really work as there are other means to call a method, such as #send and #send, unless they use "." internally? I'd prefer to see a new hook instead, something like #method_called(name) if that was your use case.

#5 Updated by Kurt Stephens over 1 year ago

I like the idea, iff:

obj.foo.bar.baz .= to_i

behaves as:

begin
temp = obj.foo.bar
temp.baz = temp.baz.to_i
end

#6 Updated by Tom Wardrop over 1 year ago

Yes, that's exactly how it would behave. To rewrite your example to make sure I understood it correctly:

obj.foo.bar.baz = obj.foo.bar.baz.to_i

becomes

obj.foo.bar.baz .= to_i

#7 Updated by Kurt Stephens over 1 year ago

If we want lexical multiplicity to equal evaluation multiplicity...

Should:

obj.foo.bar[baz.daz] .= to_i

behave as?:

begin
temp1 = obj.foo.bar
temp2 = baz.daz
temp1[temp2] = temp1[temp2].to_i
end

... Since the following evaluates "obj.bar.baz" and "baz.daz" only once:

class Obj
def foo
puts "#{self}#foo"
@foo ||= Foo.new
end
end
class Foo
def bar
puts "#{self}#bar"
@bar ||= { }
end
end
class Baz
def daz
puts "#{self}#daz"
:x
end
end
obj = Obj.new
baz = Baz.new
obj.foo.bar[baz.daz] = 1

... Similarly for below:

obj.foo.bar[baz.daz] ||= 1

#8 Updated by Tom Wardrop over 1 year ago

@kstephens, yes, that would be the expected result I believe. I don't think anyone would expect baz.daz to be called twice in that instance. Principle of least surprise applies here.

#9 Updated by Koichi Sasada over 1 year ago

  • Assignee set to Yusuke Endoh

mame-san, could you judge this ticket?

#10 Updated by Yusuke Endoh over 1 year ago

  • Status changed from Open to Assigned
  • Assignee changed from Yusuke Endoh to Yukihiro Matsumoto
  • Target version changed from 2.0.0 to Next Major

It requires matz's approval.

My personal comment: I agree, in fact I have thought the same thing
(and as I recall, unak implemented a patch).
But "..= toi" resembles a variable named `toi'. I wonder if matz
is interested or not.

Yusuke Endoh mame@tsg.ne.jp

#11 Updated by Tom Wardrop about 1 year ago

=begin
If there are concerns about using an identifier after ".=", then perhaps a symbol could be used instead:

(({obj.foo.bar.baz .= :to_i}))

That would be somewhat consistant with the alternate block syntax (not sure what it's called):

(({['a', 'b', 'c'].each &:upcase!}))
=end

#12 Updated by Martin Dürst about 1 year ago

wardrop (Tom Wardrop) wrote:

=begin
Quite often in Ruby, I find myself doing something like: (({myvar[:foo][:bar] = myvar[:foo][:bar].toi})) or (({obj.foo.bar = obj.foo.bar.toi})). Realising this, I thought of what would be a fairly nice shorthand syntax for this, which could be: (({myvar[:foo][:bar] .= toi})).

What about introducing to_i!. This would look more like Ruby.

#13 Updated by Nobuyoshi Nakada about 1 year ago

to_i! needs Object#become.

#14 Updated by Yura Sokolov about 1 year ago

=begin
May be:

(({obj.foo.bar.baz = .toi }))
(({obi.foo[bar.baz] = .to
s(16) }))

While this not consistent with ||= , it looks readable, imho.

Or combine both variants:

(({obj.foo.bar.baz .= .toi }))
(({obi.foo[bar.baz] .= .to
s(16) }))

( looks like morse ;) )
27.02.2013 4:07 пользователь "wardrop (Tom Wardrop)" tom@tomwardrop.com
написал:

Issue #6841 has been updated by wardrop (Tom Wardrop).

=begin
If there are concerns about using an identifier after ".=", then perhaps a
symbol could be used instead:

(({obj.foo.bar.baz .= :to_i}))

That would be somewhat consistant with the alternate block syntax (not
sure what it's called):

(({['a', 'b', 'c'].each &:upcase!}))

=end

Feature #6841: Shorthand for Assigning Return Value of Method to Self
https://bugs.ruby-lang.org/issues/6841#change-37122

Author: wardrop (Tom Wardrop)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: Next Major

=begin
Quite often in Ruby, I find myself doing something like:
(({myvar[:foo][:bar] = myvar[:foo][:bar].toi})) or (({obj.foo.bar =
obj.foo.bar.to
i})). Realising this, I thought of what would be a fairly
nice shorthand syntax for this, which could be: (({myvar[:foo][:bar] .=
to
i})). How this works should be pretty self-explanatory. The (({.=}))
operator works exactly like any other assignment operator of this nature.

Would be nice to see this in Ruby 2.0. Wondering what others think of this?
=end

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

#15 Updated by Nobuyoshi Nakada about 1 year ago

(13/02/28 6:16), Юрий Соколов wrote:

=begin

You can't write in RD mode via e-mail.

May be:

(({obj.foo.bar.baz = .toi }))
(({obi.foo[bar.baz] = .to
s(16) }))

While this not consistent with ||= , it looks readable, imho.

It's different from ||= etc, so it should not consistent with them.

Rather,

obj.foo.bar.baz += .toi
obi.foo[bar.baz] *= .to
s(16)

might be useful.

--
Nobu Nakada

#16 Updated by Nathan Zook about 1 year ago

http://en.wikipedia.org/wiki/Law_of_Demeter

The term that I've heard to describe obj.foo.bar.baz is "train wreck". Encouraging such seems to me to be problematic.

I am NOT saying that the transformation/normalization is wrong, just that it is being handled a the wrong level. Think about it. To make this call, you have to know about obj (which I'm assuming you should). You have to know foo about obj. You have to know bar about obj.foo. You have to know baz about obj.foo.bar. And you have to know that you want baz to be an integer. If that is not being overly intimate with your arguments, I wouldn't know what is.

If you are doing this a lot, then most likely you are normalizing a data structure. That is, your code looks like:

obj.foo.bar.baz = obj.foo.bar.baz.toi
obj.for.bar.boz = obj.foo.bar.boz.to
f
...

If so, the to_i part is CERTAINLY not the problem! My first question would be "where does obj come from"? Most likely some sort of naive deserializing process. Many deserializers are in fact quite sophisticated, and perhaps a more informed usage would bypass ever creating the object in the undesirable form.

I have a hard time imagining any other way that one would ever get to the point that obj.foo.bar.baz = obj.foo.bar.baz.to_i would make sense. But perhaps I have missed something.

#17 Updated by Tom Wardrop about 1 year ago

=begin
(({Regarding obj.foo.bar.baz = .toi})) syntax, with the dot prefix on the ((|toi|)), another proposal has been made further down the discussion for issue #8191. The idea is that an expression beginning with a dot could be an inferred method call on the result of the last expression.

Just consider that before using the dot prefix syntax for anything else.
=end

Also available in: Atom PDF