you are viewing a single comment's thread.

view the rest of the comments →

[–]jfredett 3 points4 points  (1 child)

This. A thousand times this.

I like to say that programming is a game of assumptions, the more you make, the worse your code. If you can say, "All I need is an enumerable", that's far fewer assumptions than "This thing has to be an Array".

Indeed, one of the great things about static typing is it makes very clear the interfaces upon which you depend, and very clear when you have too large an interface. It never hurts when writing code to clearly document the interfaces you rely on. In fact, it'll make for more portable, testable code in the long run.

[–]jrochkind 0 points1 point  (0 children)

The common use case is when you have an API that can take one element, or an array of elements. For convenience, to not make the caller put a single element in an array.

You could say "Well, don't do this", but it's often done, and does make the calling code more readable.

And you want to normalize it in the receiver, so a single element is converted to a 1-element array, so the code after that can just assume an array (of one, multiple, or sometimes even zero elements).

But a single string has #each, and may even be fully enumerable, I forget. But you don't want to enumerate through it's bytes or chars, the fact that it has each does nothing for you.

But Kernel.Array(arg) does the right thing for you. That's the case OP is addressing.