1. 6

    Maybe I am mis-remembering my C, but isn’t this more like ++i pre-increment? In that the expression evaluates to the incremented value? I am basing this off of the psuedocode presented of:

    class Integer
      define_method '++' do
        self = self + 1

    For which the result of the assignment is returned from this ++ method (if it worked).

    Now that I think about it, true i++ post-increment feels like very awkward Ruby, both at its object-oriented core, and in its functional flourishes. On the other hand, pre-increment fits easily as “just another method”, akin to i+=1 though possibly without the gotchas of assignment precedence.

    Post-increment has to concern itself with manipulating which immutable value object that is assigned to a variable, more than a mutation internal to an object itself. A true i++ would need to:

    1. Store the original value.
    2. Change the value assigned to the variable name, ala Binding#local_variable_set.
    3. Return the original value.

    This still likely require an MRI patch, though I haven’t though too deeply about it.

    1. 4

      Good observation, your memories of C are right. What I implemented is a weird the pre-increment operator working as postfix. I’ll update the post to acknowledge this, thanks!

      I initially implemented the pre-increment operator (++some_var) and that’d be correct because the expression evaluates to the new value of some_var. However I only changed it to be postfix for a stupid reason: it looked better to me.

      I could have gone further and implement the post-increment & post-decrement the proper way but decided to leave it there for now, doing so was a bit tricker as the expression evaluates to something different from the assigned value (as you pointed out).

      My only goal was to mess around with Ruby and challenge myself to make changes in it, there’re tons of experiments you could do for fun. For example enforcing all method calls to have parenthesis so that @ngoldbaum would like Ruby more 😜.

      1. 3

        I fully endorse your JRI fork of the ruby interpreter

    1. 11

      When the moon was young and as yet unmarked (around ruby 1.8.6)…. I asked Matz why ruby didn’t have ++

      If I remember correctly the answer was that numbers are value types in Ruby, so a mutation operator like ++ doesn’t make sense. ie. What is 1++? 1=1+1!?

      1. 2

        You’re right! Matz has always been against having this operator in Ruby.

        At first I didn’t understand why he was ok with having += but not ++, but it makes sense: Because ++ is really a variable assignment but it doesn’t look like that, so it’s confusing, whereas += has an equal that suggests the thing on the left is being assigned. It only took me a few years to get his point…

        By the way, on the patch from my blog post you can only use the ++ operator on a variable, never on a numeric literal. Just the same way you can do some_var += 1 but you can’t do 1 += 1.

        1. 1

          You could as well interpret it as a postfix version of the Successor function. It would be neither an operator (which + and - aren’t either in Ruby), nor a function that mutates its subject.