Dear messageboard users I am just wandering if there is any operator similar to the ||= for arrays? #DOES NOT WORK test_array = [] test_array ||= [''nothing...''] #DOES NOT WORK EITHER test_array = [] test_array ||<< [''nothing...''] You could write something like this: test_array << [''nothing...''] if test_array.size == 0 But that just doesn''t look half as good as any of the two above in my opinion. Best regards Sebastian Probst Eide --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/1/06, Sebastian Probst Eide <sebastian.probst.eide-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Dear messageboard users > I am just wandering if there is any operator similar to the ||= for > arrays? > > #DOES NOT WORK > test_array = [] > test_array ||= [''nothing...''] > > #DOES NOT WORK EITHER > test_array = [] > test_array ||<< [''nothing...''] > > You could write something like this: > test_array << [''nothing...''] if test_array.size == 0 > > But that just doesn''t look half as good as any of the two above in my > opinion.The ||= operator checks that the variable in question evaluates to nil (or false, I think). So, its behaving exactly as expected, since an empty array is not nil or false. -ryan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
2006/10/1, Sebastian Probst Eide <sebastian.probst.eide-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > You could write something like this: > test_array << [''nothing...''] if test_array.size == 0 >You could do:>> array = []=> []>> array << ''nothing'' if array.empty?=> ["nothing"]>> array << ''something'' if array.empty?=> nil>> array=> ["nothing"] --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sunday 01 October 2006 19:17, Sebastian Probst Eide wrote:> Dear messageboard users > I am just wandering if there is any operator similar to the ||= for > arrays? > > #DOES NOT WORK > test_array = [] > test_array ||= [''nothing...'']test_array |= [''nothing...''] Array#| is the union operator. Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Sun, 1 Oct 2006, Michael Schuerig wrote:> > On Sunday 01 October 2006 19:17, Sebastian Probst Eide wrote: >> Dear messageboard users >> I am just wandering if there is any operator similar to the ||= for >> arrays? >> >> #DOES NOT WORK >> test_array = [] >> test_array ||= [''nothing...''] > > test_array |= [''nothing...''] > > Array#| is the union operator.The problem with that (at least potentially, depending on what else is happening) is that it creates a new array object, rather than modifying test_array in place. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for all the answers! Best regards Sebastian --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---