From: Sam Duncan Date: 2010-12-14T11:03:58+09:00 Subject: Re: String or string ruby-1.9.2-p0 > u2 = "foo" => "foo" ruby-1.9.2-p0 > puts "blah" unless ["force", "spells"].include?(u2) blah => nil ruby-1.9.2-p0 > u2 = "force" => "force" ruby-1.9.2-p0 > puts "blah" unless ["force", "spells"].include?(u2) => nil ruby-1.9.2-p0 > u2 = "spells" => "spells" ruby-1.9.2-p0 > puts "blah" unless ["force", "spells"].include?(u2) => nil On 14/12/10 14:58, Garrison Taylor wrote: > I want it to puts blah if u2 isn't force or spells. > > Scratch.mit.edu. Go there! > > -gbear605 > > On Dec 13, 2010, at 7:57 PM, Ross Harvey wrote: > > >> Same problem, though. That's the same thing as >> >> if (u2 != "...") || true >> >> Which, of course, is the same thing as "if true". >> >> Did you mean: >> >> if u2 != "force"&& u2 != "spells" >> >> ...perhaps? >> >> On Mon, Dec 13, 2010 at 3:27 PM, Garrison Taylor >> wrote: >> >>> When I typed it I wasn't looking at it, it was actually >>> >>> u2 = gets.chomp >>> If u2 != "force" || "spells" >>> Puts "blah" >>> >>> Scratch.mit.edu. Go there! >>> >>> -gbear605 >>> >>> On Dec 13, 2010, at 6:09 PM, Josh Cheek wrote: >>> >>> >>>> On Mon, Dec 13, 2010 at 4:53 PM, Garrison Taylor >>>> wrote: >>>> >>>> >>>>> I am doing something where I do >>>>> If "force" || "spells" >>>>> Puts "blah" >>>>> >>>>> >>>>> >>>>> When I use it it gives a warning about comparing strings, what's happening >>>>> and how do I fix it? >>>>> >>>>> Scratch.mit.edu. Go there! >>>>> >>>>> -gbear605 >>>>> >>>>> >>>> || is a boolean method meaning "or". It is concerned with the truthiness and >>>> falsiness of its parameters. The set of falsy objects is { false , nil }, >>>> everything else is truthy. That means that strings are truthy, so when you >>>> say "force" || "spells" it looks at "force", sees it isn't false and it >>>> isn't nil, and so is considered true. Every time. >>>> >>>> So "force" || "spells" will always return "force", a truthy value, to the if >>>> statement. Thus the if statement will always execute. >>>> >>>> if "force" || "spells" >>>> puts "blah" >>>> end >>>> >>>> is the same as >>>> >>>> if "force" >>>> puts "blah" >>>> end >>>> >>>> is the same as >>>> >>>> if true >>>> puts "blah" >>>> end >>>> >>>> is the same as >>>> >>>> puts "blah" >>>> >>>> >>>> >>>> That is probably not what you wanted, so it warns you of the issue. >>>> >>> >>> >> >