Proposal for a new operator - .=

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 'with' proposal
      Hello! Wouldn't something like this be cool? class Foo def read ... end def dump ...
    2. INVESTMENT PROPOSAL
      This is a multi-part message in MIME format --dd1e7f93-6b57-489d-a349-90210943212c Content-Type: text/plain; charset=iso-8859-1...
    3. [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...
    4. [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...
    5. 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...
  3. #2

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139