|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"" 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/. > |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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/. >> > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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) |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|