On 9/18/06, Eero Saynatkari <eero.saynatkari / kolumbus.fi> wrote:
> Hi!
>
> I suppose this just qualifies as unexpected (by me) behaviour
> but an odd thing happened when I wrote code like this:
>
>  [*"foo\nbar"]   # => ["foo\n", "bar"]
>
> Mocking out the call sequence for the splat, it seems that
> it first checks for #respond_to? :to_ary and, if so, sends
> it to the object--the surprising part was that if it does
> NOT respond to :to_ary, the splat simply calls #to_a on its
> subject.
>
> The idea behind the code, of course, is to have a single
> expression that either constructs an Array or reproduces
> the current one from whatever object we are dealing with.
> This behaviour makes doing so a tad less concise :)
>

Can't you just test whether the object is an Array (or if it supports
the array methods you need)?

  x = "foo\nbar"
  (x.is_a?(Array) ? x : [x]) # => ["foo\nbar"]
  x = %w(a b c)
  (x.is_a?(Array) ? x : [x]) # => ["a", "b", "c"]


> --
> Posted via http://www.ruby-forum.com/.
>
>


-- 
- Simen