From: CHubas Date: 2007-02-23T23:35:08+09:00 Subject: Re: concatenating strings with nil values On Feb 23, 7:55 am, "tchick" 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.