Hi, I thought it''d be useful to have a method that is the opposite of Array.wrap . I''ve got this simple change drafted introducing Array.unwrap. https://github.com/carnesmedia/rails/compare/unwrap I''ve found this useful in situations where I know I''ll get a one-element array, and I just want the element, such as an api request that always returns an array even though I asked for a single result, or when I''m not sure I''ll get an array or single element (the same situation as Array.wrap except I want the single element of an array containing that element). Anyway, I''m looking for feedback. Does anyone have thoughts before I make this a pull-request? -Amiel http://carnesmedia.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
> I''ve found this useful in situations where I know I''ll get a one-element array, and I just want the element, such as an api request that always returns an array even though I asked for a single result, or when I''m not sure I''ll get an array or single element$ irb irb(main):001:0> a = 1 => 1 irb(main):002:0> Array(a) => [1] irb(main):003:0> a = [1,2,3] => [1, 2, 3] irb(main):004:0> Array(a) => [1, 2, 3] -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
I think, sadly, I have to -1 on this one. It does not clear when I see this method what would happen when you pass in array with multiple elements, or when the array unexpextedly have moret than one element. Assume that that array will always has 1 element is ... Not a good idea. I think it''s already clear and make sense to just call .first or [0] on it. Since you would be like "I don''t care how many things it has in it, I want the first one." -Prem On Thu, Sep 5, 2013 at 8:45 PM, Amiel Martin <amiel@carnesmedia.com> wrote:> Hi, > I thought it''d be useful to have a method that is the opposite of Array.wrap > . > I''ve got this simple change drafted introducing Array.unwrap. > https://github.com/carnesmedia/rails/compare/unwrap > I''ve found this useful in situations where I know I''ll get a one-element > array, and I just want the element, such as an api request that always > returns an array even though I asked for a single result, or when I''m not > sure I''ll get an array or single element (the same situation as Array.wrap > except I want the single element of an array containing that element). > Anyway, I''m looking for feedback. Does anyone have thoughts before I make > this a pull-request? > -Amiel > http://carnesmedia.com > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/groups/opt_out.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Actually, I guess it''d be $ irb irb(main):001:0> a = 1 => 1 irb(main):002:0> Array(a)[0] => 1 irb(main):003:0> a = [1,2,3] => [1, 2, 3] irb(main):004:0> Array(a)[0] => 1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Steve and Prem, I guess I thought Array.unwrap made more sense semantically in some situations, and that it''d be nice to have an opposite for Array.wrap. I can see that it doesn''t make sense to add to ActiveSupport. Thanks for reviewing, -Amiel http://carnesmedia.com On Thu, Sep 5, 2013 at 5:54 PM, Steve Klabnik <steve@steveklabnik.com>wrote:> Actually, I guess it''d be > > $ irb > irb(main):001:0> a = 1 > => 1 > irb(main):002:0> Array(a)[0] > => 1 > irb(main):003:0> a = [1,2,3] > => [1, 2, 3] > irb(main):004:0> Array(a)[0] > => 1 > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/groups/opt_out. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
On Sep-06-2013, at 6:15 AM, Amiel Martin <amiel@carnesmedia.com> wrote:> I''ve found this useful in situations where I know I''ll get a one-element array, and I just want the element, such as an api request that always returns an array even though I asked for a single result, or when I''m not sure I''ll get an array or single element (the same situation as Array.wrap except I want the single element of an array containing that element).I''ve been on a project where we had added a method something like the following class Array def only_element raise ''array has too many elements'' if length > 1 first end end and so we used to get the only element out of arrays that way. Having an array in which you are sure there is only a single element is something that many would call a smell. However, I think this is off topic for Rails core now. -- Tejas Dinkar http://www.nilenso.com Nilenso Software (formerly C42 Engineering) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.