Hey Active Support defines Array.wrap (copied below). What''s the difference between that and the Ruby builtin Array() method: Array(nil) # => [] Array([0]) # => [0] class C def to_ary [0] end end Array(C.new) # => [0] Array(0) # => [0] Perhaps it was just an overlook that Array() exists? -- fxn class Array # Wraps the object in an Array unless it''s an Array. Converts the # object to an Array using #to_ary if it implements that. def self.wrap(object) case object when nil [] when self object else if object.respond_to?(:to_ary) object.to_ary else [object] end end end end
IIRC, it''s different on 1.9. On Mon, Aug 24, 2009 at 10:20 PM, Xavier Noria<fxn@hashref.com> wrote:> > Hey Active Support defines Array.wrap (copied below). What''s the > difference between that and the Ruby builtin Array() method: > > Array(nil) # => [] > Array([0]) # => [0] > > class C > def to_ary > [0] > end > end > Array(C.new) # => [0] > > Array(0) # => [0] > > Perhaps it was just an overlook that Array() exists? > > -- fxn > > class Array > # Wraps the object in an Array unless it''s an Array. Converts the > # object to an Array using #to_ary if it implements that. > def self.wrap(object) > case object > when nil > [] > when self > object > else > if object.respond_to?(:to_ary) > object.to_ary > else > [object] > end > end > end > end > > > >-- Cheers! - Pratik http://m.onkey.org
There''s also:>> Array(:a=>:b)=> [[:a, :b]]>>Mighty annoying. On Tue, Aug 25, 2009 at 9:33 AM, Pratik<pratiknaik@gmail.com> wrote:> > IIRC, it''s different on 1.9. > > On Mon, Aug 24, 2009 at 10:20 PM, Xavier Noria<fxn@hashref.com> wrote: >> >> Hey Active Support defines Array.wrap (copied below). What''s the >> difference between that and the Ruby builtin Array() method: >> >> Array(nil) # => [] >> Array([0]) # => [0] >> >> class C >> def to_ary >> [0] >> end >> end >> Array(C.new) # => [0] >> >> Array(0) # => [0] >> >> Perhaps it was just an overlook that Array() exists? >> >> -- fxn >> >> class Array >> # Wraps the object in an Array unless it''s an Array. Converts the >> # object to an Array using #to_ary if it implements that. >> def self.wrap(object) >> case object >> when nil >> [] >> when self >> object >> else >> if object.respond_to?(:to_ary) >> object.to_ary >> else >> [object] >> end >> end >> end >> end >> >> > >> > > > > -- > Cheers! > - Pratik > http://m.onkey.org > > > >-- Cheers Koz
On Mon, Aug 24, 2009 at 11:33 PM, Pratik<pratiknaik@gmail.com> wrote:> IIRC, it''s different on 1.9.Ah I tried as well with same result: [xnoria@ruby-versions ~]$ irb191 irb(main):001:0> Array(nil) => [] irb(main):002:0> Array(0) => [0] irb(main):003:0> Array([0]) => [0] ... irb(main):007:0> irb(main):008:0* class C irb(main):009:1> def to_ary irb(main):010:2> [0] irb(main):011:2> end irb(main):012:1> end => nil irb(main):013:0> Array(C.new) => [0]
On Mon, Aug 24, 2009 at 2:33 PM, Pratik<pratiknaik@gmail.com> wrote:> > IIRC, it''s different on 1.9. >I don''t think its a 1.9 thing. It had something to do with strings being treated as Enumerable. See this:>> Array("helo \n world")=> ["helo \n", " world"]>> Array.wrap("helo \n world")=> ["helo \n world"] -Mark
On Mon, Aug 24, 2009 at 2:20 PM, Xavier Noria<fxn@hashref.com> wrote:> > Hey Active Support defines Array.wrap (copied below). What''s the > difference between that and the Ruby builtin Array() method: > > Array(nil) # => [] > Array([0]) # => [0] > > class C > def to_ary > [0] > end > end > Array(C.new) # => [0] > > Array(0) # => [0] > > Perhaps it was just an overlook that Array() exists?Array("foo\nbar") # => ["foo\n", "bar"] Array.wrap("foo\nbar") # => ["foo\nbar"] jeremy
On Mon, Aug 24, 2009 at 2:50 PM, Xavier Noria<fxn@hashref.com> wrote:> > On Mon, Aug 24, 2009 at 11:33 PM, Pratik<pratiknaik@gmail.com> wrote: > >> IIRC, it''s different on 1.9. > > Ah I tried as well with same result:Array() does too much. We want a uniform wrapper with no surprises. jeremy
On Mon, Aug 24, 2009 at 11:38 PM, Michael Koziarski<michael@koziarski.com> wrote:> > There''s also: > >>> Array(:a=>:b) > => [[:a, :b]] >>> > > Mighty annoying.Ah yes, Array() calls #to_a also.
On Mon, Aug 24, 2009 at 11:58 PM, Jeremy Kemper<jeremy@bitsweat.net> wrote:> Array("foo\nbar") # => ["foo\n", "bar"] > > Array.wrap("foo\nbar") # => ["foo\nbar"]Excellent I''ll the hash example and those to the RDoc and AS guide.
On Tue, Aug 25, 2009 at 12:25 AM, Jeremy Kemper<jeremy@bitsweat.net> wrote:> Array() does too much. We want a uniform wrapper with no surprises.Understood. I wanted to know the difference because I am documenting it. According to the Pickaxe and Flanafan & Matz the difference is exactly that Array.wrap does not call to_a, and those examples depict this.
On Tue, Aug 25, 2009 at 12:40 AM, Xavier Noria<fxn@hashref.com> wrote:> Flanafan & MatzUgh sorry, Flanagan.
On Mon, Aug 24, 2009 at 11:33 PM, Pratik<pratiknaik@gmail.com> wrote:> IIRC, it''s different on 1.9.That''s true, for example the return value of Array("foo\nbar") depends on the Ruby version because strings are not enumerables in 1.9. So you get ["foo\nbar"] in 1.9, and ["foo\n", "bar"] in 1.8. So Array.wrap is portable as long as +to_ary+ is portable.