Hi, Is there an easy way to change the way ruby outputs nil''s. Such that "" is output instead? The reason for this, is due to the amount of space a large array can occupy if containing several thousand empty cells as opposed to one which prints out "". Thanks, -- 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 -~----------~----~----~----~------~----~------~--~---
WKC CCC said the following on 02/05/2007 12:53 PM:> Hi, > > Is there an easy way to change the way ruby outputs nil''s. Such that "" > is output instead? The reason for this, is due to the amount of space a > large array can occupy if containing several thousand empty cells as > opposed to one which prints out "".Why don''t you use a "sparse array"? Any CompSci textbook will discuss this. How do you think spreadsheet programs like Excel store so many cells on small Windwos machines? -- "I don''t mind a parasite, I object to a cut-rate one" - Cassablanca --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You can use to_s function to replace NIL to empty string. For example, you can put following code in your view. <%= obj.to_s %> If obj is NIL, it simply returns "" (empty string), and you can avoid runtime errors. Cheers, Glenn On Feb 5, 7:53 am, WKC CCC <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > Is there an easy way to change the way ruby outputs nil''s. Such that "" > is output instead? The reason for this, is due to the amount of space a > large array can occupy if containing several thousand empty cells as > opposed to one which prints out "". > > Thanks, > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Glenn wrote:> You can use to_s function to replace NIL to empty string. > For example, you can put following code in your view. > <%= obj.to_s %> > If obj is NIL, it simply returns "" (empty string), and you can avoid > runtime errors. > > Cheers, > GlennThanks, however using to_s removes the structure of the array on display and concats the whole array into a single string. For example: testarray = [["one", nil, "three"], [nil, "five","six"]] How can I get this so that the ouput is displayed as: [["one","","three"], ["","five","six"]] So that it can deal with generic objects containing nil items. Thanks, -- 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 -~----------~----~----~----~------~----~------~--~---
WKC CCC wrote:> How can I get this so that the ouput is displayed as: > [["one","","three"], > ["","five","six"]]testarray.map{|q| q.to_s } This might even work: testarray.map(&:to_s) -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Feb 8, 2007, at 12:01 PM, Phlip wrote:> WKC CCC wrote: > >> How can I get this so that the ouput is displayed as: >> [["one","","three"], >> ["","five","six"]] > > testarray.map{|q| q.to_s } > > This might even work: > > testarray.map(&:to_s) > > -- > Phlip > http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!testarray.inspect.gsub(''nil'', ''""'') If you need each element of the original array to being on its own line, you''ll have to roll your own. If you do that, then each element could be: (element || "").inspect except that turns both nil and false to the "" string. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.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?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> testarray.inspect.gsub(''nil'', ''""'')That idea is ... disturbing on many levels. To begin, what happens when one of the string elements is ''Manila''? -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Phlip wrote:> Rob Biedenharn wrote: > >> testarray.inspect.gsub(''nil'', ''""'') > > That idea is ... disturbing on many levels. To begin, what happens > when one of the string elements is ''Manila''? > > -- > Phlip > http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!That example works - thanks Rob. In the case of ''manila'' manila is still printed out or whatever string content the variable may hold -- 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 -~----------~----~----~----~------~----~------~--~---
WKC CCC wrote:> In the case of ''manila'' manila is still printed out or whatever string > content the variable may holdp ''manila''.gsub(''nil'', ''ni'') -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Feb 8, 2007, at 2:30 PM, Phlip wrote:> WKC CCC wrote: >> In the case of ''manila'' manila is still printed out or whatever >> string >> content the variable may hold > > p ''manila''.gsub(''nil'', ''ni'') > > -- > Phlip > http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!;-) Since it wasn''t *my* problem, I tried not to go crazy looking for a solution. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.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?hl=en -~----------~----~----~----~------~----~------~--~---
> Is there an easy way to change the way ruby outputs nil''s. Such that "" > is output instead? The reason for this, is due to the amount of space a > large array can occupy if containing several thousand empty cells as > opposed to one which prints out "".Alternately, you can override the inspect method: class NilClass def inspect "!" # I prefer a bang, to empty quotes end end -Kyle --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Kyle Maxwell wrote:>> Is there an easy way to change the way ruby outputs nil''s. Such that "" >> is output instead? The reason for this, is due to the amount of space a >> large array can occupy if containing several thousand empty cells as >> opposed to one which prints out "". > > Alternately, you can override the inspect method: > > class NilClass > def inspect > "!" # I prefer a bang, to empty quotes > end > end > > -KyleThanks! -- 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 -~----------~----~----~----~------~----~------~--~---