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.
0395dce49ee7fa03ad9c10e8105d9245?d=identicon&s=25 WKC CCC (wkc)
on 2007-02-05 18: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,
140c065a9604b5321768cbe02e91ef30?d=identicon&s=25 Anton Aylward (Guest)
on 2007-02-05 19: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
7cd9ea64fa126c4e38573828d43df911?d=identicon&s=25 Glenn (Guest)
on 2007-02-06 05: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
0395dce49ee7fa03ad9c10e8105d9245?d=identicon&s=25 WKC CCC (wkc)
on 2007-02-08 17: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,
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-02-08 18: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!!
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-02-08 18: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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-02-08 18:50
(Received via mailing list)
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!!
0395dce49ee7fa03ad9c10e8105d9245?d=identicon&s=25 WKC CCC (wkc)
on 2007-02-08 18:59
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
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-02-08 20: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!!
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-02-08 21: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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
4a06d387963f5d1a0f49671153cb8866?d=identicon&s=25 Kyle Maxwell (Guest)
on 2007-02-09 00: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
0395dce49ee7fa03ad9c10e8105d9245?d=identicon&s=25 WKC CCC (wkc)
on 2007-02-09 12:37
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
>
> -Kyle

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