Forum: Ruby on Rails nil output

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
WKC C. (Guest)
on 2007-02-05 19:53
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,
Anton A. (Guest)
on 2007-02-05 20:19
(Received via mailing list)
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
Glenn (Guest)
on 2007-02-06 06:18
(Received via mailing list)
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
WKC C. (Guest)
on 2007-02-08 18:56
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,
> Glenn

Thanks, 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,
Phlip (Guest)
on 2007-02-08 19:03
(Received via mailing list)
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!!
Rob B. (Guest)
on 2007-02-08 19:43
(Received via mailing list)
On Feb 8, 2007, at 12:01 PM, Phlip wrote:
> 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 B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
Phlip (Guest)
on 2007-02-08 19:50
(Received via mailing list)
Rob B. 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!!
WKC C. (Guest)
on 2007-02-08 19:59
Phlip wrote:
> Rob B. 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
Phlip (Guest)
on 2007-02-08 21:30
(Received via mailing list)
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!!
Rob B. (Guest)
on 2007-02-08 22:15
(Received via mailing list)
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 B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
Kyle M. (Guest)
on 2007-02-09 01:56
(Received via mailing list)
> 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
WKC C. (Guest)
on 2007-02-09 13:37
Kyle M. 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
>
> -Kyle

Thanks!
This topic is locked and can not be replied to.