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 “”.
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”
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.
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.
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.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.