hmm... after finally discovering that Array.to_a doesn't double-array now I'm getting warnings that Array.to_a is being obsoleted (with the unhelpful message: warning: default `to_a' will be obsolete). Aunty google tells me I should now use Array(thing)!
Can I just say (from a purely aesthetic POV):
yuk!
When you use to_a you can just read your code left-to-right eg:
Thing.do_stuff(args).to_a.do_something_else
You get a clear idea of what order your messages are passed in.
but having to use a stonking great Array() around one section means you have to jump back to the left again!
Array(Thing.do_stuff(args)).do_something_else
You have to read left-right-left-right :P
It's not as pretty.
So why exactly are they obsoleting Array.to_a ???
6 comments:
There's an argument that objects of one type shouldn't need to know how to convert themselves to other types; it should be the responsibility of the type itself to know how to convert objects.
If you've ever come across a situation where there are dozens of to_* methods for a class, you can see why some people consider it a problem.
Wait, _what_? You're telling me that Array should know how to convert some random class that I just wrote into an array? How is it supposed to do that?
I think you've got me backwards.. I'm saying that ['a','b'].to_a should exist - but it's deprecated.
Chris: I think that argument just moves the problem - doesn't solve it. It just means that an Array (a major, world-spanning kind of object) now has to know how to convert a whole bunch of other objects that may be quite obscure... and means that you need to hash on the Array class itself to implement a from_my_object for every object you own...
I don't see this as a net plus.
I can understand there'd be a problem if you find yourself implementing a proliferation of to_my_special_object methods - but there are some simple types that people use all the time, and I'd argue that many objects should match these interfaces eg to_a, to_s etc... where relevant.
There's nothing stopping you from not implementing that interface... but IMO the base classes in ruby (Array, Hash etc) should implement these so we have a way of converting from simple things to other simple things.
and besides all that... I did make my argument from an aesthetic preference ;)
Still - now I have a few purely functional arguments to add to the case too :)
Of course, this is still ruby.
When it is deprecated, this should make all our lovely codes still worky-worky if loaded suitably early:
class Object
def to_a
Array(self)
end
end
Yoink! At least, thats what i'd do if some near sighted foos removed my stuffs.
As an aside, i also like and prefer the LTR reading aspect, its very ruby and Array(something) is quite antithematic IMHO.
Just my two cents.
Post a Comment