[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Joshua Muheim

11/7/2007 12:42:00 AM

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
--
Posted via http://www.ruby-....

19 Answers

Pradeep Elankumaran

11/7/2007 1:10:00 AM

0

"" or "asdf"
nil or "asdf"


On Nov 6, 2007, at 7:41 PM, Joshua Muheim 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? :-)
>
> Thanks
> Josh
> --
> Posted via http://www.ruby-....
>


Pradeep Elankumaran

11/7/2007 1:13:00 AM

0

actually, that doesn't work. sorry.

On Nov 6, 2007, at 8:10 PM, Pradeep Elankumaran wrote:

> "" or "asdf"
> nil or "asdf"
>
>
> On Nov 6, 2007, at 7:41 PM, Joshua Muheim 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? :-)
>>
>> Thanks
>> Josh
>> --
>> Posted via http://www.ruby-....
>>
>


Jeremy Woertink

11/7/2007 1:28:00 AM

0

Joshua Muheim 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? :-)
>
> Thanks
> Josh

I guess you could do something cool like

class String

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

end


:)


~Jeremy
--
Posted via http://www.ruby-....

Joshua Muheim

11/7/2007 1:32:00 AM

0

Konrad Meyer wrote:
> Quoth Joshua Muheim:
>>
>> 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
--
Posted via http://www.ruby-....

yermej

11/7/2007 1:33:00 AM

0

On Nov 6, 6:41 pm, Joshua Muheim <fo...@josh.ch> 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? :-)

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

Todd Benson

11/7/2007 2:06:00 AM

0

On 11/6/07, yermej <yermej@gmail.com> wrote:
> On Nov 6, 6:41 pm, Joshua Muheim <fo...@josh.ch> 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? :-)
>
> "" 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

Todd Benson

11/7/2007 2:25:00 AM

0

On 11/6/07, Todd Benson <caduceass@gmail.com> wrote:
> On 11/6/07, yermej <yermej@gmail.com> wrote:
> > On Nov 6, 6:41 pm, Joshua Muheim <fo...@josh.ch> 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? :-)
> >
> > "" 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

> end

Todd

Phrogz

11/7/2007 4:53:00 AM

0

On Nov 6, 6:31 pm, Joshua Muheim <fo...@josh.ch> 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


Rob Biedenharn

11/7/2007 5:27:00 AM

0

On Nov 6, 2007, at 9:25 PM, Todd Benson wrote:
> On 11/6/07, Todd Benson <caduceass@gmail.com> wrote:
>> On 11/6/07, yermej <yermej@gmail.com> wrote:
>>> On Nov 6, 6:41 pm, Joshua Muheim <fo...@josh.ch> 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? :-)
>>>
>>> "" 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
>
>> end
>
> 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://agileconsult...
Rob@AgileConsultingLLC.com


Robert Klemme

11/7/2007 12:44:00 PM

0

2007/11/7, Rob Biedenharn <Rob@agileconsultingllc.com>:
> On Nov 6, 2007, at 9:25 PM, Todd Benson wrote:
> > On 11/6/07, Todd Benson <caduceass@gmail.com> wrote:
> >> On 11/6/07, yermej <yermej@gmail.com> wrote:
> >>> On Nov 6, 6:41 pm, Joshua Muheim <fo...@josh.ch> 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? :-)
> >>>
> >>> "" 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
> >
> >> end
> >
> > 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

--
use.inject do |as, often| as.you_can - without end