[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Proposal for a new operator - .=

Ryan Pavlik

11/17/2003 10:04:00 AM

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. ;-)

--
Ryan Pavlik <rpav@mephle.com>

"Every day shall be sword day." - 8BT

1 Answer

Robert Klemme

11/17/2003 4:17:00 PM

0


"Ryan Pavlik" <rpav@mephle.com> schrieb im Newsbeitrag
news:20031117020352.788607da.rpav@mephle.com...
> 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