[libxml-devel] << operator
Sean Chittenden
sean at chittenden.org
Thu Aug 21 15:13:10 EDT 2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
> This code:
>
> node = XML::Node.new('foo') << XML::Node.new('bar') << "bars contents"
>
> Generates this xml:
>
> puts node
> <foo><bar/>bars contents</foo>
>
> That is because the << returns self.
That's bad.
> Eric Musgrove if instead it should work like this (see http://rubyforge.org/tracker/?func=detail&atid=1971&aid=21424&group_id=494)
> :
>
> puts node.parent.parent
>
> <foo>
> <bar>bars contents</bar>
> <foo>
>
> By making the << operator return the appended node.
>
> << returning the appended node seems more useful, but doesn't follow
> the Ruby standard as pointed out by Masashi Shimbo.
>
> Thoughts?
I have all kinds of issues with "the Ruby standard" and think this is
an opportune place to break that convention. The path of least
resistance should win (i.e. the programmer is right, do what's easiest
for the programmer).
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
@#$@#$@# @#$@#$ @#$@# @#$@# @#$@#$ @#$@ing @#$#@@!!!!!!!
Ignore the "Ruby standard." -sc
- --
Sean Chittenden
sean at chittenden.org
-----BEGIN PGP SIGNATURE-----
iEYEARECAAYFAkitvkcACgkQTrydwWwuXhaEhACdHk8MlSI+hLfji+tUVm1DMGGo
ZAgAniAAKqXGozIAyATQl+o4EFU9fGyf
=9VR5
-----END PGP SIGNATURE-----
More information about the libxml-devel
mailing list