Matz says that he didn't support ++ (or --) in Ruby because he
couldn't cleanly fit it into his model of how OO worked.
> > irb(main):017:0> a++
> > irb(main):018:0* a
> > => 2
The * on line 18 shows that irb hasn't seen the end of the statement,
so it reprompts for the rest of the statement. I believe a ++ a is
being parsed as
a + (+ a). Putting it all on one line makes this a bit clearer :
irb(main):006:0> a = 1
=> 1
irb(main):007:0> a ++ a
=> 2
irb(main):008:0> a + (+a)
=> 2
irb(main):009:0> a
=> 1
Wayne
---
Wayne Vucenic
No Bugs Software
"Agile Ruby Contract Programming in Silicon Valley since 2001"
On 10/3/07, Billy <[address removed]> wrote:
> Ruby does not support ++ or -- .. it has something to do with the way that
> '+' is parsed
>
> On 10/3/07, Ed Allen < [address removed]> wrote:
> >
> >
> > Here's something I don't understand, that was tripping me up and
> generating a bug:
> >
> >
> >
> > irb(main):016:0> a = 1
> > => 1
> > irb(main):017:0> a++
> > irb(main):018:0* a
> > => 2
> > irb(main):019:0> b = 0
> > => 0
> > irb(main):020:0> b++
> > irb(main):021:0* b
> > => 0
> > irb(main):022:0> c = 0
> > => 0
> > irb(main):023:0> c = c + 1
> > => 1
> > irb(main):024:0> c
> > => 1
> >
> >
> >
> >
> >
> >
> > Why doesn't ++ add 1 to 0 but it does add to a non-zero integer?
> >
> >
> > Clearly it doesn't do what I expected it to do, an unusual case of Ruby's
> p rinciple of least surprise (POLS) being violated.
> >
> >
> > Experimenting a bit more, I discover
> >
> >
> > irb(main):025:0> a= -1
> > => -1
> > irb(main):026:0> a++
> > irb(main):027:0* a
> > => -2
> >
> >
> > which also seems wrong to me.
> >
> >
> >
> >
> >
> > --
> > Please Note: If you hit "REPLY", your message will be sent to everyone on
> this mailing list ( [address removed])
> > This message was sent by Ed Allen ([address removed]) from The East Bay
> Ruby Meetup Group.
> > To learn more about Ed Allen, visit his/her member profile
> > To unsubscribe or to update your mailing list settings, click here
> >
> > Meetup.com Customer Service: [address removed]
> > 632 Broadway New York NY 10012 USA
>
>
>
>
>
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on
> this mailing list ([address removed])
> This message was sent by Billy ([address removed]) from The East Bay Ruby
> Meetup Group.
> To learn more about Billy, visit his/her member profile
> To unsubscribe or to update your mailing list settings, click here
>
> Meetup.com Customer Service: [address removed]
> 632 Broadway New York NY 10012 USA