[libxml-devel] << operator

Sean Chittenden sean at chittenden.org
Thu Aug 21 15:53:06 EDT 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>>>> A contrived example, but this code absolutely irritates the shit  
>>>> out of me:
>>>> Class F
>>>> def a=(b)
>>>>   @z = b + 1
>>>>   return @z
>>>> end
>>>> def a
>>>>   return @z
>>>> end
>>>> end
>>>> f = F.new
>>>> x = f.a = 1
>>>> p f.a  # Prints 2
>>>> p x    # Prints 1
>>>
>>> That's bizarre.  What is going on there?  Print statement show  
>>> that def a= is called before def a, so can x ever be set to 1?
>> It's an "optimization".  The rvalue of an assignment is the  
>> argument to all lvalues in an assignment statement.  i.e.: x = f.a  
>> = 1 is translated into "x = 1 ; f.a = 1".  I think that was a very  
>> bad decision, but oh well.  Ruby ain't perfect and this, to me at  
>> least, is perfect justification for doing what is right vs. what is  
>> "standard."  -sc
>
> Ah.  That is silly.


And because it is a counterintuitive construct compared to nearly  
every other language on the planet, it is VERY difficult to figure out  
for the first time!!!!!  Ugh.  -sc

- --
Sean Chittenden
sean at chittenden.org



-----BEGIN PGP SIGNATURE-----

iEYEARECAAYFAkitx6IACgkQTrydwWwuXhbJ/ACeLd18kCsGW2aPx8JXVVKLoTn2
IIoAoI4rTXZ5jYtcrSk76OifSgcKK/DV
=82es
-----END PGP SIGNATURE-----


More information about the libxml-devel mailing list