On Oct 29, 9:18=A0pm, RichardOnRails
<RichardDummyMailbox58... / USComputerGurus.com> wrote:
> Hi,
>
> In The Well Grounded Rubyist (which, =A0along with The Ruby Way, I
> love), =A0Dave Black presents a justification for the x++ omission in
> Ruby. =A0I 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. =A0I wasn=92t willing to delve into Matz=92
> implementation of higher-level Ruby functions or low-level C/C++
> code. =A0Below is what I came up with.
>
> Q1. =A0Test 1 failed, I imagine, =A0because the interpret thought =93Ah, =
7=92s
> a Fixnum which I =91ve seen billions of times, so I know what to do with
> it.=94 =A0Thus, =A0it ignored my over-riding definition of Fixnum. =A0So =
I
> call it =93compiler error=94, to use the vernacular, =A0by reporting that=
 x
> had no method =93pp=94. =A0Am I all wet?
>
> Test 2 passed, =A0but 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. =A0Am I all wet?
>
> Q3. y.inspect yielded only it=92s value (8) and nothing else,
> suggesting the x.pp yielded a full-fledged Fixnum. =A0 =A0Am 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. =A0This example somewhat supports that view, =A0but 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
> =A0 attr :val
> =A0 def initialize (fixnum)
> =A0 =A0 puts "Initializing object %d" % fixnum
> =A0 =A0 raise "Initializer not a Fixnum" if
> =A0 =A0 =A0 =A0 fixnum.class !=3D Fixnum
> =A0 =A0 puts "\nInitializing Fixnum to %d" % fixnum
> =A0 =A0 @val =3D fixnum
> =A0 end
>
> =A0 def pp
> =A0 =A0 @val+=3D1
> =A0 end
> end
>
> # Test 1 (Failure)
> x =3D 7; =A0y =3D x.pp; z =3Dx.class # undefined method `pp' for 7:Fixnum
> (NoMethodError)
>
> # Test 2 (Success)
> x =3D FixNum.new(7); =A0y =3D x.pp; z =3Dx.class
> puts [x.to_s, y.inspect, z.to_s].join("; ") # =3D> <FixNum:0x2b62694>;
> 8; FixNum

Hi,

Please ignore my questions.  I just discovered a major mistake: I
thought I was overriding Ruby's Fixnum class,  but I misspelled it at
FixNum.  That error invalidates most if not all of the things I
reported.  Mea Culpa.

I'll correct my errors and probably have a new theory subsequently.

Best wishes,
Richard