Dot-equals method?

Hello,

I keep finding myself wanting to write things like this:

string.=reverse
input.=split /\s*/

instead of this:

string = string.reverse
input = input.split /\s*/

It’s a silly thing, I guess, but after years of programming in Ruby I
still find myself trying to do this… so maybe there’s something to this.

Is there a good reason why this couldn’t be done? (Does it break some
syntax somewhere, or just not make sense?) Would it be possible to do
something like this without hacking the interpreter?

Thanks,

Chris

string.reverse!

?

···

On Sat, Jan 11, 2003 at 05:05:26AM +0900, Chris Pine wrote:

string.=reverse


---- WBR, Michael Shigorin mike@altlinux.ru
------ Linux.Kiev http://www.linux.kiev.ua/

Hello,

I keep finding myself wanting to write things like this:

string.=reverse
input.=split /\s*/

instead of this:

string = string.reverse
input = input.split /\s*/

I understand. But in these two cases I guess
you’d want reverse! and split! instead.

It’s a silly thing, I guess, but after years of programming in Ruby I
still find myself trying to do this… so maybe there’s something to
this.

Is there a good reason why this couldn’t be done? (Does it break some
syntax somewhere, or just not make sense?) Would it be possible to do
something like this without hacking the interpreter?

I’ve thought about this before. I think you’d
probably have to use a symbol for the method,
to distinguish from a toplevel method call or
self.meth, but then the parameters wouldn’t
make sense.

All in all, I think it would cause more trouble
than it helped us. Just my opinion.

Hal

···

----- Original Message -----
From: “Chris Pine” nemo@hellotree.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, January 10, 2003 2:05 PM
Subject: dot-equals method?

I understand. But in these two cases I guess
you’d want reverse! and split! instead.

···

----- Original Message -----
From: “Hal E. Fulton” hal9000@hypermetrics.com


Yeah, ok, those were really dumb examples! Plus, why would you split a
string on /\s*/ ??

However, there is no split!' method, nor could there ever be, sincesplit’
is a string method returning an array. That’s what I like so much about the
`.=’ idea.

If something like this were possible, it seems like we wouldn’t even need
the `!’ methods anymore. Well, I would never use them, but maybe other
people still would if, for some reason, they wanted to change the object
itself… like if other variables were referencing it or something… seems
like kind of screwbally design to me, but what do I know? :slight_smile:

----- Original Message -----
I’ve thought about this before. I think you’d
probably have to use a symbol for the method,
to distinguish from a toplevel method call or
self.meth, but then the parameters wouldn’t
make sense.

You mean if we did this in Ruby (as opposed to hacking the interpreter)?
Hmm… you’re probably right.

Chris

Nitpickily, it’s not the same thing -

string = “hello world”
a = string
string = string.reverse

martin

···

Hal E. Fulton hal9000@hypermetrics.com wrote:

From: “Chris Pine” nemo@hellotree.com

string = string.reverse
input = input.split /\s*/

I understand. But in these two cases I guess
you’d want reverse! and split! instead.

Hi –

However, there is no split!' method, nor could there ever be, since split’
is a string method returning an array. That’s what I like so much about the
`.=’ idea.

It doesn’t make visual or semantic sense to me; it’s neither clearly a
method call nor an assignment. I guess the idea is that it’s sort of
both… but I have trouble perceiving it that way. For example:

str.= upcase

What that looks like, to me, at most, is a kind of variant on:

str = self.upcase

with ‘=’ taking message/method syntax.

If something like this were possible, it seems like we wouldn’t even need
the `!’ methods anymore. Well, I would never use them, but maybe other
people still would if, for some reason, they wanted to change the object
itself… like if other variables were referencing it or something… seems
like kind of screwbally design to me, but what do I know? :slight_smile:

Well, yeah, changing the object itself is the point of the bang
methods :slight_smile: I don’t think the thing you’re talking about has a bearing
on that; it seems to be more a kind of assignment shorthand.

David

···

On Sat, 11 Jan 2003, Chris Pine wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

However, there is no split!' method, nor could there ever be, since split’
is a string method returning an array. That’s what I like so much about the
`.=’ idea.

It doesn’t make visual or semantic sense to me;
[snip]
Well, yeah, changing the object itself is the point of the bang
methods :slight_smile: I don’t think the thing you’re talking about has a bearing
on that; it seems to be more a kind of assignment shorthand.

I don’t see how it’s much worse than *=, +=, etc.
These don’t change the method (like !) but are assignment shorthands.
Like you said, this is precisely what a ‘.=’ would do. That’s also why it
would be acceptable for it to change the object type as in

something .= split /\s+/

I personally like the idea of ‘.=’ very much.

Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Hi –

However, there is no split!' method, nor could there ever be, since split’
is a string method returning an array. That’s what I like so much about the
`.=’ idea.

It doesn’t make visual or semantic sense to me;
[snip]
Well, yeah, changing the object itself is the point of the bang
methods :slight_smile: I don’t think the thing you’re talking about has a bearing
on that; it seems to be more a kind of assignment shorthand.

I don’t see how it’s much worse than *=, +=, etc.
These don’t change the method (like !) but are assignment shorthands.

The . has a somewhat different status than + or *, though.

x += 1       ==     x = x.+ 1
x *= 1       ==     x = x.* 1

x .= split   ?=     x = x.. split

so (the way I perceive it) there’s an extra level of collapsing syntax
into shortcut in the .= case. (Mind you, exact correspondence to +=
and *= isn’t necessarily a deciding factor.)

David

···

On Sat, 11 Jan 2003, Daniel Carrera wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

However, there is no split!' method, nor could there ever be, since split’
is a string method returning an array. That’s what I like so much
about the
`.=’ idea.

It doesn’t make visual or semantic sense to me;
[snip]
Well, yeah, changing the object itself is the point of the bang
methods :slight_smile: I don’t think the thing you’re talking about has a bearing
on that; it seems to be more a kind of assignment shorthand.

I don’t see how it’s much worse than *=, +=, etc.
These don’t change the method (like !) but are assignment shorthands.
Like you said, this is precisely what a ‘.=’ would do. That’s also why it
would be acceptable for it to change the object type as in

something .= split /\s+/

I personally like the idea of ‘.=’ very much.

First of all, of course I was mistaken about ‘split!’
Sorry, I’ve just returned from a tour of the
small German village of Brehnfahrt.

As for .= … I just think it’s too magic.

If I say
obj .= foo
is that calling obj.foo or a toplevel foo? What if
both exist, which do you pick?

What about other method calls in the expression?
obj .= foo + bar
Does that mean
obj = obj.foo + obj.bar
or
obj = obj.foo + bar
or what?

Would you allow this?
obj .= +(3)
instead of
obj += 3
? No one would want to do that, but it’s consistent
to allow it.

I just think it raises more questions than it answers.

However, I like the general kind of thinking that
produces this kind of thing, even though I don’t
believe it pans out in this instance.

Hal

···

----- Original Message -----
From: “Daniel Carrera” dcarrera@math.umd.edu
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, January 10, 2003 3:49 PM
Subject: Re: dot-equals method?

The . has a somewhat different status than + or *, though.

x += 1       ==     x = x.+ 1
x *= 1       ==     x = x.* 1

x .= split   ?=     x = x.. split

Why can’t we say:

x += 2 == x = x + 2
x *= 2 == x = x + 2
x .= foo == x = x . foo

I don’t see the extra collapsing syntax (yes I realize that ‘.’ is
different from ‘+’, but syntatically they are used the same).

Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

What about other method calls in the expression?
obj .= foo + bar
Does that mean
obj = obj.foo + obj.bar
or
obj = obj.foo + bar
or what?

···

----- Original Message -----
From: “Hal E. Fulton” hal9000@hypermetrics.com


Definitely not the former!

Hmmm… I’m trying to figure out what someone would be trying to do with an
expression like that:

str = ‘olleh’
str .= reverse + ’ world’

Something like that? Yeah… it is kind of weird. The only logical thing I
can think of is for `str’ to be ‘hello’ after this, but for the expression
to return ‘hello world’. This certainly does not jive with something like:

num += 5 + 2

…where `num’ is 7, not 5.

Good point! It’s kind of neat, but clearly breaks the Principle of Least
Surprise in anything but the simplest of uses. This is what I was looking
for.

Thanks,

Chris

If I say
obj .= foo
is that calling obj.foo or a toplevel foo? What if
both exist, which do you pick?

obj .= foo is shorthand for obj = obj . foo

What about other method calls in the expression?
obj .= foo + bar

I think that it’s a matter of presedence. Should this be interpreted as

(obj .= foo) + bar
or
obj .= (foo + bar)

The first wouldn’t make sense, so it should be the latter. Thus, we get
the equivalent of :

obj = obj . (foo + bar)

I can’t imagine how this could be used in a real program, but I think it’s
the logically correct conclusion. It’s the same reason why, when I write:

num /= 3 + 2

It gets interpreted as

num = num / (3 + 2)

and not

num = (num / 3) + 2
or
num = (num / 3) + (num / 2)

Would you allow this?
obj .= +(3)
instead of
obj += 3
? No one would want to do that, but it’s consistent
to allow it.

I would allow it yes.

I can see your point. However, I think I’m still inclined in favour of
‘.=’. The ‘.=’ has a unique, logically consistent, interpretation.

Chris mentioned:

str = ‘olleh’
str .= reverse + ’ world’

I’m not sure if someone would try to do that. I can’t see how (s)he
could expect this to produce the desired result. There are two possible
ways to parse this depending on presedence:

(str .= reverse) + ’ world’
and
str .= (reverse + ’ world’)

Neither would produce what (s)he wants.

Most people think that this is ambiguous, and that probably means that it
is. But I just don’t see why the parsing is ambiguous.

Just my $0.02

Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Why can’t we say:

x += 2 == x = x + 2
x *= 2 == x = x + 2
x .= foo == x = x . foo

···

----- Original Message -----
From: “Daniel Carrera” dcarrera@math.umd.edu

The . has a somewhat different status than + or *, though.

x += 1       ==     x = x.+ 1
x *= 1       ==     x = x.* 1

x .= split   ?=     x = x.. split

Well, I think the point that he was making was that:

x += 2 means “call x’s `+’ method, passing in 2”

which, if used in the same way, gives:

x .= foo means “call x’s `.’ method, passing in foo”

which, of course, makes no sense.

That fact that it looks like the expansion is so similiar (as you showed
in your email) is, I think, why it just feels right at first. I think I
have started thinking of .' almost like it's a method, too, but it isn't. Perhaps this comes from thinking of methods and their parameters as simply messages passed. Maybe this all started because I've been playing with method_missing’ too much! After a while, even `def’ started to feel like
fluff.

Time to do something else,

:slight_smile:

Chris

‘2’ is an argument to +, whereas ‘foo’ is a method.

As was pointed out in another post, in the op= methods, the entire right
hand side is treated as an argument to the operator, whereas . can only
take a single method name as an ‘argument’.

martin

···

Daniel Carrera dcarrera@math.umd.edu wrote:

Why can’t we say:

x += 2 == x = x + 2
x *= 2 == x = x + 2
x .= foo == x = x . foo

I don’t see the extra collapsing syntax (yes I realize that ‘.’ is
different from ‘+’, but syntatically they are used the same).