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

J

Joshua Muheim

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? :)

Thanks
Josh
 
J

Jeremy Woertink

Joshua said:
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? :)

Thanks
Josh

I guess you could do something cool like

class String

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

end


:)


~Jeremy
 
J

Joshua Muheim

Konrad said:
Quoth Joshua Muheim:

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
 
Y

yermej

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? :)

"" 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)
 
T

Todd Benson

"" 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
 
T

Todd Benson

"" 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 )

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

Todd
 
P

Phrogz

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
 
R

Rob Biedenharn

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? :)

"" 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 )

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

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:in `nonblank?'
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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Robert Klemme

2007/11/7 said:
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? :)

"" 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 )

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

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:in `nonblank?'
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;"

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
 
J

Joshua Muheim

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!
 
P

Phrogz

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.
 
R

Rob Biedenharn

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!


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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top