Is there a "||" that treats "" also as false?

Hi all

irb(main):001:0> “” || “asdf”
=> “”
irb(main):002:0> nil || “asdf”
=> “asdf”
irb(main):003:0>

I’d like the first one to also return “asdf”. So is there an operator
that fits my needs? :slight_smile:

Thanks
Josh

“” or “asdf”
nil or “asdf”

Quoth Joshua M.:

Thanks
Josh

irb(main):001:0> str = “”
=> “”
irb(main):002:0> str != “” || “asdf”
=> “asdf”
irb(main):003:0> nil || “asdf”
=> “asdf”

HTH,

Joshua M. wrote:

Hi all

irb(main):001:0> “” || “asdf”
=> “”
irb(main):002:0> nil || “asdf”
=> “asdf”
irb(main):003:0>

I’d like the first one to also return “asdf”. So is there an operator
that fits my needs? :slight_smile:

Thanks
Josh

I guess you could do something cool like

class String

def |(str)
return str if self.eql?("")
end

end

:slight_smile:

~Jeremy

Konrad M. wrote:

Quoth Joshua M.:

Thanks
Josh

irb(main):001:0> str = “”
=> “”
irb(main):002:0> str != “” || “asdf”
=> “asdf”
irb(main):003:0> nil || “asdf”
=> “asdf”

HTH,

The problem is, in my Rails app I sometimes have a variable set to “”
(empty user input) or nil.

<%= “The variable is #{(var || “empty”)}” %>

With your version I won’t get “empty” when the var is nil:

<%= “The variable is #{(var != “” || “empty”)}” %>

var = nil
=> The variable is # nothing here…
var = “asdf”
=> The variable is asdf

actually, that doesn’t work. sorry.

On Nov 6, 6:41 pm, Joshua M. [email protected] wrote:

Hi all

irb(main):001:0> “” || “asdf”
=> “”
irb(main):002:0> nil || “asdf”
=> “asdf”
irb(main):003:0>

I’d like the first one to also return “asdf”. So is there an operator
that fits my needs? :slight_smile:

“” isn’t false so || and or won’t work. You’ll have to code it
differently. E.g.:

a = “a string”
a || (a.empty? ? “asdf” : a)

On 11/6/07, yermej [email protected] wrote:

that fits my needs? :slight_smile:

“” isn’t false so || and or won’t work. You’ll have to code it
differently. E.g.:

a = “a string”
a || (a.empty? ? “asdf” : a)

This doesn’t work for me. The previous one doesn’t either. This one
does (I’m sure someone could easily clean this up, I feel lazy
though)…

[nil, “”, “something”].each do |i|
puts( (item ||= “”).empty? ? “asdf” : item )
end

That results in…

asdf
asdf
something

Todd

On 11/6/07, Todd B. [email protected] wrote:

I’d like the first one to also return “asdf”. So is there an operator
though)…

[nil, “”, “something”].each do |i|
puts( (item ||= “”).empty? ? “asdf” : item )

There should be an additional closing ) on the previous line of code

end

Todd

On Nov 6, 6:31 pm, Joshua M. [email protected] wrote:

The problem is, in my Rails app I sometimes have a variable set to “”
(empty user input) or nil.

This is why ActiveSupport has the #blank? method:

irb(main):001:0> require ‘rubygems’
=> true
irb(main):002:0> require ‘active_support’
=> true
irb(main):003:0> nil.blank?
=> true
irb(main):004:0> “”.blank?
=> true
irb(main):005:0> “foo”.blank?
=> false
irb(main):006:0> false.blank?
=> true
irb(main):007:0> 0.blank?
=> false

On Nov 6, 2007, at 9:25 PM, Todd B. wrote:

This doesn’t work for me. The previous one doesn’t either. This one
Todd
I use these extensions in several projects for exactly the same reason
as the OP

class String

Allowing a chain like: string_value.nonblank? || ‘default value’

def nonblank?
self unless blank?
end
end

class NilClass

Allowing a chain like: value.nonblank? || ‘default value’

def nonblank?
self
end

so it plays nicely with Numeric#nonzero?

def nonzero?
self
end
end

irb(main):018:0> “”.nonblank? || “asdf”
NoMethodError: undefined method blank?' for "":String from (irb):4:innonblank?’
from (irb):18

Ok, so these are typically Rails projects, but you can take the
String#blank? extension from ActiveSupport

irb(main):019:0> class String #:nodoc:
irb(main):020:1> def blank?
irb(main):021:2> empty? || strip.empty?
irb(main):022:2> end
irb(main):023:1> end
=> nil
irb(main):024:0>

And then Joshua’s orignal examples become:

irb(main):025:0* “”.nonblank? || “asdf”
=> “asdf”
irb(main):026:0> nil.nonblank? || “asdf”
=> “asdf”

And for completeness:

irb(main):027:0> “jkl;”.nonblank? || “asdf”
=> “jkl;”

-Rob

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

2007/11/7, Rob B. [email protected]:

irb(main):003:0>

end
end
irb(main):019:0> class String #:nodoc:
=> “asdf”
irb(main):026:0> nil.nonblank? || “asdf”
=> “asdf”

And for completeness:

irb(main):027:0> “jkl;”.nonblank? || “asdf”
=> “jkl;”

Why not simply define a global method?

def substitute_default(s, fallback)
s.nil? || s == “” ? fallback : s
end

The name of course is just a suggestion.

Kind regards

robert

On Nov 7, 10:36 am, Joshua M. [email protected] wrote:

class String
def |(alternative)
(self.nil? or self == “”) ? alternative : self
end
end

An instance of a String class will never be an instance of NilClass
instead. You can remove “self.nil? or” from the above.

I finally came up with this:

class String
def |(alternative)
(self.nil? or self == “”) ? alternative : self
end
end

class NilClass
def |(alternative)
alternative
end
end

Is this OK? Or do I accidently override some existing method? Because
without the definition above

nil | “asdf”

returns

true!

On Nov 7, 2007 6:52 PM, Rob B. [email protected]
wrote:

That’s because NilClass#| is already defined so you probably should
leave it alone.
Hey this is cool, it seems that
!!x == nil | x
:wink:
R.

On Nov 7, 2007, at 12:36 PM, Joshua M. wrote:

alternative
true!
That’s because NilClass#| is already defined so you probably should
leave it alone.

For the String part, you’ll only ever get truth from self.nil? if self
really is nil so that method would be better refactored as:

class String
def |(alternative)
empty? ? alternative : self
end
end

-Rob

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