Nil output

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,

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 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

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,

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. 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!!

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
[email protected]

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

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!!

:wink:

Since it wasn’t my problem, I tried not to go crazy looking for a
solution.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

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!!

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!

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