This post originated from an RSS feed registered with Ruby Buzz
by Eigen Class.
Original Post: Preincrement plays, not about arithmetics actually
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
One of the first things people often ask for when coming to Ruby is a ++ operator.
The standard answer to that has always been that such a thing makes no sense
in Ruby, since you don't want
1++
to turn literal ones into
twos*1.
But one thing one doesn't hear that often is that preincrement/predecrement
operators can be simulated pretty straightforwardly with these semantics:
I'll show an example involving arithmetic with boxed integers, but
preincrement/decrement should never be used for that. I can only picture
this being of use in some
DSL*2.
This takes around 30 lines and it's a pretty interesting exercise, so
you might want to try to write it on your own before I give my code.
Here are some test cases that will tell you when you're done: