Proposal for a new operator - .=

R

Ryan Pavlik

OK so it's late, I'm not thinking straight, and I thought I'd post
this.

Proposal for a new operator - .=
================================

Ruby has a number of useful operators, including some for combined
operation/assignment, such as +=, -=, etc.

This proposal is for a new operation/assignment operator, .=
(period-equals). Existing operators follow this form:

a += b # => a = a + b
a -= b # => a = a - b

The proposed .= operator would follow in an identical fashion:

a .= b # => a = a.b

For instance, some practical applications:

# We're passed an object, we want to do work on it
def foo(x)
x .= dup
:
end

# We have an operation for which there is no bang! method:
hash .= invert

# Similar to the last, we have an object which a bang method
# is impossible:
i = 5
i .= succ # => 6

# Following from that, we can't really change what an object _is_,
# but with .= we don't need to:
c = 65
c .= chr # => "A"


This has the following important advantages:

* It provides a generic operation/assignment operator

* Much like existing operation/assignment operators, it saves
space and increases readability

* It confuses Perl programmers

Support this new operator for Ruby!
---------------------------------------------------------------------------

(The preceding was 90% a joke. I don't actually have any serious
expectation of a .= operator in Ruby. I imagine it would be a bit
hairy to implement. I actually thought of it after doing "s = s.dup"
a few times, and there may be a few good uses for it, but... this was
for entertainment purposes only. ;-)
 
R

Robert Klemme

Ryan Pavlik said:
OK so it's late, I'm not thinking straight, and I thought I'd post
this.

Proposal for a new operator - .=
================================

Ruby has a number of useful operators, including some for combined
operation/assignment, such as +=, -=, etc.

This proposal is for a new operation/assignment operator, .=
(period-equals). Existing operators follow this form:

a += b # => a = a + b
a -= b # => a = a - b

The proposed .= operator would follow in an identical fashion:

a .= b # => a = a.b

For instance, some practical applications:

# We're passed an object, we want to do work on it
def foo(x)
x .= dup
:
end

# We have an operation for which there is no bang! method:
hash .= invert

# Similar to the last, we have an object which a bang method
# is impossible:
i = 5
i .= succ # => 6

# Following from that, we can't really change what an object _is_,
# but with .= we don't need to:
c = 65
c .= chr # => "A"


This has the following important advantages:

* It provides a generic operation/assignment operator

* Much like existing operation/assignment operators, it saves
space and increases readability

* It confuses Perl programmers

Support this new operator for Ruby!
------------------------------------------------------------------------ ---

(The preceding was 90% a joke. I don't actually have any serious
expectation of a .= operator in Ruby. I imagine it would be a bit
hairy to implement. I actually thought of it after doing "s = s.dup"
a few times, and there may be a few good uses for it, but... this was
for entertainment purposes only. ;-)

:))

This becomse more interesting with multiple method invocations:

foo = foo.bar.baz # becometh
foo .= bar.baz

foo.bar = foo.bar.baz # now is
foo.bar .= baz

Weired...

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top