PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Is there a "||" that treats "" also as false?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Is there a "||" that treats "" also as false?

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2007, 00h41   #1
Joshua Muheim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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? :-)

Thanks
Josh
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 01h10   #2
Pradeep Elankumaran
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a "||" that treats "" also as false?

"" 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-forum.com/.
>



  Réponse avec citation
Vieux 07/11/2007, 01h13   #3
Pradeep Elankumaran
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a "||" that treats "" also as false?

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-forum.com/.
>>

>



  Réponse avec citation
Vieux 07/11/2007, 01h27   #4
Jeremy Woertink
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a "||" that treats "" also as false?

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-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 01h31   #5
Joshua Muheim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a "||" that treats "" also as false?

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-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 01h33   #6
yermej
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a "||" that treats "" also as false?

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)

  Réponse avec citation
Vieux 07/11/2007, 02h06   #7
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a "||" that treats "" also as false?

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

  Réponse avec citation
Vieux 07/11/2007, 02h25   #8
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a "||" that treats "" also as false?

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h07.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19481 seconds with 16 queries