my_array = [ 5, 3, 4, [ "abc", 3, 5 ], [ "x", 5 ] ] i want to search for "x" do you have to loop thru the array to search this item or is there some easier way to do this. i would like to delete x from the array to achieve this. my_array_deleted = [ 5, 3, 4, [ "abc", 3, 5 ] ] thanks --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Do you really want to remove any sub-array in which "x" occurs? my_array_deleted = [ 5, 3, 4, [ "abc", 3, 5 ] ] Or, do you want to remove only any elements that equal "x"? my_array_deleted = [ 5, 3, 4, [ "abc", 3, 5 ], [ 5 ] ] I just want to be sure that you really want to also remove the 5 that''s in the same sub-array as "x" in your example. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
opps your right, i want to delete the sub-array so the 5 would be deleted as well. gosh, your reading my mind. On Apr 19, 5:47 am, "Craig Demyanovich" <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Do you really want to remove any sub-array in which "x" occurs? > my_array_deleted = [ 5, 3, 4, [ "abc", 3, 5 ] ] > > Or, do you want to remove only any elements that equal "x"? > > my_array_deleted = [ 5, 3, 4, [ "abc", 3, 5 ], [ 5 ] ] > > I just want to be sure that you really want to also remove the 5 that''s in > the same sub-array as "x" in your example. > > Regards, > Craig--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi -- On Sat, 19 Apr 2008, rushnosh wrote:> On Apr 19, 5:47am, "Craig Demyanovich" <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> Do you really want to remove any sub-array in which "x" occurs? >> my_array_deleted = [ 5, 3, 4, [ "abc", 3, 5 ] ] >> >> Or, do you want to remove only any elements that equal "x"? >> >> my_array_deleted = [ 5, 3, 4, [ "abc", 3, 5 ], [ 5 ] ] >> >> I just want to be sure that you really want to also remove the 5 that''s in >> the same sub-array as "x" in your example. >> > > opps your right, i want to delete the sub-array so the 5 would be > deleted as well.The first thing my fingers come up with (and it''s unlikely to be the most efficient -- it never seems to be :-) is this: my_array.delete_if {|a| [*a].include?("x") } => [5, 3, 4, ["abc", 3, 5]] David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
it works. can you explain *a thanks On Apr 19, 7:18 am, "David A. Black" <dbl...-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org> wrote:> Hi -- > > > > On Sat, 19 Apr 2008, rushnosh wrote: > > On Apr 19, 5:47 am, "Craig Demyanovich" <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> Do you really want to remove any sub-array in which "x" occurs? > >> my_array_deleted = [ 5, 3, 4, [ "abc", 3, 5 ] ] > > >> Or, do you want to remove only any elements that equal "x"? > > >> my_array_deleted = [ 5, 3, 4, [ "abc", 3, 5 ], [ 5 ] ] > > >> I just want to be sure that you really want to also remove the 5 that''s in > >> the same sub-array as "x" in your example. > > > opps your right, i want to delete the sub-array so the 5 would be > > deleted as well. > > The first thing my fingers come up with (and it''s unlikely to be the > most efficient -- it never seems to be :-) is this: > > my_array.delete_if {|a| [*a].include?("x") } > => [5, 3, 4, ["abc", 3, 5]] > > David > > -- > Rails training from David A. Black and Ruby Power and Light: > INTRO TO RAILS June 9-12 Berlin > ADVANCING WITH RAILS June 16-19 Berlin > INTRO TO RAILS June 24-27 London (Skills Matter) > Seehttp://www.rubypal.comfor details and updates!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sat, Apr 19, 2008 at 11:10 AM, rushnosh <rashantha-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > it works. > can you explain *aMy very basic understanding of the splat operator (*) on an array is that it expands the array. Here''s an example IRB session.>> a = [1,2,3]=> [1, 2, 3]>> puts *a1 2 3 => nil>>David knows Ruby very, very well. Perhaps he''ll expand on my very simple explanation. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David A. Black wrote:> my_array.delete_if {|a| [*a].include?("x") } > => [5, 3, 4, ["abc", 3, 5]]The splat here really isn''t necessary. The * unpacks an array. It is often used with argument to methods: def foo(a, b, c) puts "a:#{a} b:#{b} c:#{c}" end my_array = [1,2,3] foo(*my_array) #=> "a:1 b:2 c:3" So the splat lets you pass an array as individual arguments. Or it can work the other way around. def foo(*args) puts "arguments: #{args.size}" end foo(1,2,3) #=> 3 This example allows individual arguments to be passed in as an array. This pattern is used a lot throughout rails. In David''s example: my_array.delete_if {|a| [*a].include?("x") } The "a" in that code will be each subarray of this main array. *a will unpack that array into individual elements. And the [] wrapped around *a will make a new array containing those elements. This all means that: a = [1,2,3] a == [*a] #=> true So, the simpler way to do this is just: my_array.delete_if {|a| a.include?("x") } Hopefully that explains the splat a bit. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi -- On Sat, 19 Apr 2008, Alex Wayne wrote:> > David A. Black wrote: >> my_array.delete_if {|a| [*a].include?("x") } >> => [5, 3, 4, ["abc", 3, 5]] > > The splat here really isn''t necessary.Yes it is (see below).> In David''s example: > > my_array.delete_if {|a| [*a].include?("x") } > > The "a" in that code will be each subarray of this main array. *a will > unpack that array into individual elements. And the [] wrapped around > *a will make a new array containing those elements. > > This all means that: > > a = [1,2,3] > a == [*a] #=> true > > So, the simpler way to do this is just: > > my_array.delete_if {|a| a.include?("x") }Except the array my_array doesn''t consist only of subarrays; it also contains some scalar elements. Doing it your way, you''d end up calling (for example) 5.include?. That''s why I un-arrayed and re-arrayed each element. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David A. Black wrote:> On Sat, 19 Apr 2008, Alex Wayne wrote: > >> >> David A. Black wrote: >>> my_array.delete_if {|a| [*a].include?("x") } >>> => [5, 3, 4, ["abc", 3, 5]] >> >> The splat here really isn''t necessary. > > Yes it is (see below). > >> a = [1,2,3] >> a == [*a] #=> true >> >> So, the simpler way to do this is just: >> >> my_array.delete_if {|a| a.include?("x") } > > Except the array my_array doesn''t consist only of subarrays; it also > contains some scalar elements. Doing it your way, you''d end up calling > (for example) 5.include?.Well said. My bad. Nice catch. I suppose I would have done something like: a = [a] unless a.is_a?(Array) But your code is definitely shorter and cleaner. Nice. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
thanks, for the excellent explanation. On Apr 19, 11:26 am, Alex Wayne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> David A. Black wrote: > > On Sat, 19 Apr 2008, Alex Wayne wrote: > > >> David A. Black wrote: > >>> my_array.delete_if {|a| [*a].include?("x") } > >>> => [5, 3, 4, ["abc", 3, 5]] > > >> The splat here really isn''t necessary. > > > Yes it is (see below). > > >> a = [1,2,3] > >> a == [*a] #=> true > > >> So, the simpler way to do this is just: > > >> my_array.delete_if {|a| a.include?("x") } > > > Except the array my_array doesn''t consist only of subarrays; it also > > contains some scalar elements. Doing it your way, you''d end up calling > > (for example) 5.include?. > > Well said. My bad. Nice catch. I suppose I would have done something > like: > > a = [a] unless a.is_a?(Array) > > But your code is definitely shorter and cleaner. Nice. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---