[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

concatenating strings with nil values

tchick

2/23/2007 1:56:00 PM

Hi *,

I like to concatenate a string out of some variables, some of which
are nil. Instead of getting always a "can't convert nil into String
(TypeError)" message, I would like to have "nil" appended to my
string, as in Java null objects are printed as null in Strings.
Something like this:

words = Hash.new
words[0]="one"
allWords = "all words:"
allWords << words[0]
allWords << words[1]
# --> can't convert nil into String (TypeError)

Is this possible? Or how can I handle nil objects for this case?

Thanks.

7 Answers

Harry

2/23/2007 2:19:00 PM

0

On 2/23/07, tchick <monsorno-nospam@gmx.de> wrote:
> Hi *,
>
> I like to concatenate a string out of some variables, some of which
> are nil. Instead of getting always a "can't convert nil into String
> (TypeError)" message, I would like to have "nil" appended to my
> string, as in Java null objects are printed as null in Strings.
> Something like this:
>
> words = Hash.new
> words[0]="one"
> allWords = "all words:"
> allWords << words[0]
> allWords << words[1]
> # --> can't convert nil into String (TypeError)
>
> Is this possible? Or how can I handle nil objects for this case?
>
> Thanks.
>

Is this what you are looking for?

http://www.rubycentral.com/book/ref_c_hash.htm...

Harry
--
http://www.kakueki.com/ruby...

Rubén Medellín

2/23/2007 2:31:00 PM

0

On Feb 23, 7:55 am, "tchick" <monsorno-nos...@gmx.de> wrote:
> Hi *,
>
> I like to concatenate a string out of some variables, some of which
> are nil. Instead of getting always a "can't convert nil into String
> (TypeError)" message, I would like to have "nil" appended to my
> string, as in Java null objects are printed as null in Strings.
> Something like this:
>
>
> Is this possible? Or how can I handle nil objects for this case?
>
> Thanks.

Hi.
There are some ways to do it. The simplest I can suggest is:

allWords << (w[1] || "nil")

with the disadvantage you'd have to state it everywhere you expect a
nil value.

Another way is

class String
alias old_concat <<
def <<(something)
something = "nil" if something.nil?
old_concat(something)
end
end

in which case is not recommended to mess up with core classes.

tchick

2/23/2007 2:33:00 PM

0

On 23 Feb., 15:19, Harry <ruby.hardw...@gmail.com> wrote:
> Is this what you are looking for?
>
> http://www.rubycentral.com/book/ref_c_hash.htm...

Ahem, no.

What I'm actually trying is this: I have an array of hashes,
and I like to make strings out of them. Some of the hashes
do not have values for some keys, so when putting together
the string, I get the error message, but I just want to add
"nil" or something else for this case.

Trans

2/23/2007 2:44:00 PM

0



On Feb 23, 9:00 am, "tchick" <monsorno-nos...@gmx.de> wrote:
> Hi *,
>
> I like to concatenate a string out of some variables, some of which
> are nil. Instead of getting always a "can't convert nil into String
> (TypeError)" message, I would like to have "nil" appended to my
> string, as in Java null objects are printed as null in Strings.
> Something like this:
>
> words = Hash.new
> words[0]="one"
> allWords = "all words:"
> allWords << words[0]
> allWords << words[1]
> # --> can't convert nil into String (TypeError)
>
> Is this possible? Or how can I handle nil objects for this case?

If you're adventerous (ie. your code is essentially standalone and in
no danger of effecting others):

class NilClass
def to_str
""
end
end

T.


Pit Capitain

2/23/2007 2:53:00 PM

0

tchick schrieb:
> On 23 Feb., 15:19, Harry <ruby.hardw...@gmail.com> wrote:
>> Is this what you are looking for?
>>
>> http://www.rubycentral.com/book/ref_c_hash.htm...
>
> Ahem, no.

Are you sure? See below...

> What I'm actually trying is this: I have an array of hashes,
> and I like to make strings out of them. Some of the hashes
> do not have values for some keys, so when putting together
> the string, I get the error message, but I just want to add
> "nil" or something else for this case.

words = Hash.new "nil" # I suppose this is what Harry thought of
words[0]="one"

allWords = "all words:"
allWords << words[0]
allWords << words[1]

p allWords # => "all words:onenil"

Regards,
Pit

tchick

2/23/2007 3:04:00 PM

0

Puh, why do I always forget that /this/ is possible in Ruby.
It's hard to get rid of those "safety handcuffs" ...

Thanks!

tchick

2/23/2007 3:13:00 PM

0

> words = Hash.new "nil" # I suppose this is what Harry thought of

Oh yes, _that's_ good! You're right, I didn't get it the first time!

Thanks.