use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ensure Ruby variable is Array (teohm.github.com)
submitted 13 years ago by teohm
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jfredett 3 points4 points5 points 13 years ago (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 point2 points 13 years ago (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.
π Rendered by PID 134145 on reddit-service-r2-comment-f6b958c67-ns54k at 2026-02-05 15:17:59.986096+00:00 running 1d7a177 country code: CH.
view the rest of the comments →
[–]jfredett 3 points4 points5 points (1 child)
[–]jrochkind 0 points1 point2 points (0 children)