Ask a Question related to Ruby, Design and Development.
-
Ryan Pavlik #1
Proposal for a new operator - .=
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
Ryan Pavlik Guest
-
'with' proposal
Hello! Wouldn't something like this be cool? class Foo def read ... end def dump ... -
INVESTMENT PROPOSAL
This is a multi-part message in MIME format --dd1e7f93-6b57-489d-a349-90210943212c Content-Type: text/plain; charset=iso-8859-1... -
[PHP-DEV] URGENT PROPOSAL
MRS=2E EKI OMORODION # 8 Queens Drive Ikoyi Lagos=2E Email=3Aekiomorodion670=40netscape=2Enet INTRODUCTION=3A l am Mrs=2E Eki Omorodion l know... -
[PHP-DEV] Feature proposal
I have to fight with people that using error suppression operator - they just hide their bugs. I wrote simple patch that allows set... -
Proposal system
Am I beginning to develop a proposal system that will be types in the word, which would the best way of me be to interact with that problem, in... -
Robert Klemme #2
Re: Proposal for a new operator - .=
"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
Robert Klemme Guest



Reply With Quote

