addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcredit-cardcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1launch-new-window--smalllight-bulblinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

What's up with ++?

From: Ed A.
Sent on: Wednesday, October 3, 2007 10:43 AM
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 principle 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.

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy