[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

"str1" == "STR1" case insensitive

Shea Martin

3/21/2006 6:56:00 PM

For doing case insensitive comparisons, I have been using

if str1.casecmp(str2)==0
puts 'equal'
else
puts 'not equal'
end

Is their a way to do this with simpler syntax?

~S
7 Answers

Jacob Fugal

3/21/2006 7:07:00 PM

0

On 3/21/06, Shea Martin <null@void.0> wrote:
> For doing case insensitive comparisons, I have been using
>
> if str1.casecmp(str2)==0
> puts 'equal'
> else
> puts 'not equal'
> end
>
> Is their a way to do this with simpler syntax?

I find String#downcase convenient for these sorts of comparisons:

if str1.downcase == str2.downcase
puts 'equal'
else
puts 'not equal'
end

Jacob Fugal


13

3/21/2006 7:25:00 PM

0

Hi,

My suggestion is to write a simple convinience method such as:

def are_equal(str1, str2)
str1.casecmp(str2) == 0
end

irb(main):023:0> if are_equal('foo', 'Foo')
irb(main):024:1> puts 'equal'
irb(main):025:1> else
irb(main):026:1* puts 'not equal'
irb(main):027:1> end
equal

--
Martins

On 3/21/06, Jacob Fugal <lukfugl@gmail.com> wrote:
> On 3/21/06, Shea Martin <null@void.0> wrote:
> > For doing case insensitive comparisons, I have been using
> >
> > if str1.casecmp(str2)==0
> > puts 'equal'
> > else
> > puts 'not equal'
> > end
> >
> > Is their a way to do this with simpler syntax?
>
> I find String#downcase convenient for these sorts of comparisons:
>
> if str1.downcase == str2.downcase
> puts 'equal'
> else
> puts 'not equal'
> end
>
> Jacob Fugal
>
>


Pete

3/21/2006 7:38:00 PM

0

class String
def eq(arg)
self.casecmp(arg ) == 0
end
end

puts "Test".eq("test")
> true




> --- Ursprüngliche Nachricht ---
> Von: "Jacob Fugal" <lukfugl@gmail.com>
> An: ruby-talk@ruby-lang.org (ruby-talk ML)
> Betreff: Re: "str1" == "STR1" case insensitive
> Datum: Wed, 22 Mar 2006 04:06:44 +0900
>
> On 3/21/06, Shea Martin <null@void.0> wrote:
> > For doing case insensitive comparisons, I have been using
> >
> > if str1.casecmp(str2)==0
> > puts 'equal'
> > else
> > puts 'not equal'
> > end
> >
> > Is their a way to do this with simpler syntax?
>
> I find String#downcase convenient for these sorts of comparisons:
>
> if str1.downcase == str2.downcase
> puts 'equal'
> else
> puts 'not equal'
> end
>
> Jacob Fugal
>


Chris Hulan

3/21/2006 7:57:00 PM

0

Peter Ertl wrote:
> class String
> def eq(arg)
> self.casecmp(arg ) == 0
> end
> end
>

Or you could say:

class String
alias eq casecmp
end

> puts "Test".eq("test")
> > true

Pete

3/21/2006 8:13:00 PM

0

indeed good idea, but it's not the same:

class String
alias eq2 casecmp
def eq1(arg)
self.casecmp(arg ) == 0
end
end


puts "equal1.1" if "A".eq1("a")
puts "equal2.1" if "A".eq2("a")

puts "equal1.2" if "A".eq1("b")
puts "equal2.2" if "A".eq2("b")

> equal1.1
> equal2.1
> equal2.2


> --- Ursprüngliche Nachricht ---
> Von: "ChrisH" <chris.hulan@gmail.com>
> An: ruby-talk@ruby-lang.org (ruby-talk ML)
> Betreff: Re: "str1" == "STR1" case insensitive
> Datum: Wed, 22 Mar 2006 04:58:50 +0900
>
> Peter Ertl wrote:
> > class String
> > def eq(arg)
> > self.casecmp(arg ) == 0
> > end
> > end
> >
>
> Or you could say:
>
> class String
> alias eq casecmp
> end
>
> > puts "Test".eq("test")
> > > true
>
>


Pavel Smerk

3/22/2006 1:10:00 PM

0

Peter Ertl wrote:
> class String
> def eq(arg)
> self.casecmp(arg ) == 0
> end
> end
>
> puts "Test".eq("test")
>
>>true

And how can I define eq as an binary operator on String-s allowing me to
write "Test" eq "test"? (And if this is possible, how can set a priority
for such operator?)

P.

>>--- Ursprüngliche Nachricht ---
>>On 3/21/06, Shea Martin <null@void.0> wrote:
>>
>>>For doing case insensitive comparisons, I have been using
>>>
>>>if str1.casecmp(str2)==0
>>> puts 'equal'
>>>else
>>> puts 'not equal'
>>>end
>>>
>>>Is their a way to do this with simpler syntax?
>>
>>I find String#downcase convenient for these sorts of comparisons:
>>
>> if str1.downcase == str2.downcase
>> puts 'equal'
>> else
>> puts 'not equal'
>> end
>>
>>Jacob Fugal
>>
>
>
>

Pete

3/22/2006 1:49:00 PM

0

My understanding is that you can only overload existing operators
like ==, !, === but not define custom ones.

Operator overloading should be handled carefully in any case.

I think it is good thing that custom operators
are not allowed / possible.


> --- Ursprüngliche Nachricht ---
> Von: Pavel Smerk <smerk@fi.muni.cz>
> An: ruby-talk@ruby-lang.org (ruby-talk ML)
> Betreff: Re: "str1" == "STR1" case insensitive
> Datum: Wed, 22 Mar 2006 22:23:53 +0900
>
> Peter Ertl wrote:
> > class String
> > def eq(arg)
> > self.casecmp(arg ) == 0
> > end
> > end
> >
> > puts "Test".eq("test")
> >
> >>true
>
> And how can I define eq as an binary operator on String-s allowing me to
> write "Test" eq "test"? (And if this is possible, how can set a priority
> for such operator?)
>
> P.
>
> >>--- Ursprüngliche Nachricht ---
> >>On 3/21/06, Shea Martin <null@void.0> wrote:
> >>
> >>>For doing case insensitive comparisons, I have been using
> >>>
> >>>if str1.casecmp(str2)==0
> >>> puts 'equal'
> >>>else
> >>> puts 'not equal'
> >>>end
> >>>
> >>>Is their a way to do this with simpler syntax?
> >>
> >>I find String#downcase convenient for these sorts of comparisons:
> >>
> >> if str1.downcase == str2.downcase
> >> puts 'equal'
> >> else
> >> puts 'not equal'
> >> end
> >>
> >>Jacob Fugal
> >>
> >
> >
> >
>