Converting an empty string into nil in Ruby
Posted
by adi92
on Stack Overflow
See other posts from Stack Overflow
or by adi92
Published on 2009-03-15T05:59:52Z
Indexed on
2010/05/25
21:31 UTC
Read the original article
Hit count: 266
I have a string called word
and a function called infinitive
such that
word.infinitive
would return another string on some occasions and an empty string otherwise
I am trying to find an elegant ruby one line expression for the code-snippet below
if word.infinitive == ""
return word
else return word.infinitive
Had infinitive returned nil instead of "", I could have done something like
(word.infinitive or word)
But since it does not, I can't take advantage of the short-circuit OR
Ideally I would want
1) a single expression that I could easily embed in other code
2) the function infinitive being called only once
3) to not add any custom gems or plugins into my code
© Stack Overflow or respective owner