Hi,

In The Well Grounded Rubyist (which,  along with The Ruby Way, I
love),  Dave Black presents a justification for the x++ omission in
Ruby.  I thought I'd see whether I could implement just the
incrementing portion of C's x++ functionality (without the prefix/
postfix issues) in pure Ruby.  I wasn=92t willing to delve into Matz=92
implementation of higher-level Ruby functions or low-level C/C++
code.  Below is what I came up with.

Q1.  Test 1 failed, I imagine,  because the interpret thought =93Ah, 7=92s
a Fixnum which I =91ve seen billions of times, so I know what to do with
it.=94  Thus,  it ignored my over-riding definition of Fixnum.  So I
call it =93compiler error=94, to use the vernacular,  by reporting that x
had no method =93pp=94.  Am I all wet?

Test 2 passed,  but it with a couple of anomalies:

Q2. x.to_s yielded x=92s class and address, suggesting x had become a
=93semi-Fixed Num=94, i.e, x now stored a REFERENCE to the to x=92s data,
rather than the immediate storage of the data, as interpreter is wont
to do with Fixnum=92s.  Am I all wet?

Q3. y.inspect yielded only it=92s value (8) and nothing else,
suggesting the x.pp yielded a full-fledged Fixnum.    Am I all wet?

Dave Black argues that since Fixnum values use immediate storage
rather than referenced storage, x=3D1;x++ would be akin to changing 1
into 2.  This example somewhat supports that view,  but the
interpreter sidestepped the matter by creating a reference for x=92s
value and an immediate value for y (=3Dx.pp).

Any ideas,
Richard


class FixNum
  attr :val
  def initialize (fixnum)
    puts "Initializing object %d" % fixnum
    raise "Initializer not a Fixnum" if
	fixnum.class !=3D Fixnum
    puts "\nInitializing Fixnum to %d" % fixnum
    @val =3D fixnum
  end

  def pp
    @val+=3D1
  end
end

# Test 1 (Failure)
x =3D 7;  y =3D x.pp; z =3Dx.class # undefined method `pp' for 7:Fixnum
(NoMethodError)

# Test 2 (Success)
x =3D FixNum.new(7);  y =3D x.pp; z =3Dx.class
puts [x.to_s, y.inspect, z.to_s].join("; ") # =3D> <FixNum:0x2b62694>;
8; FixNum