Forum: Ruby Case sensitive strings

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf25fbf53c67e27d95845e77e949b56f?d=identicon&s=25 Stuart Clarke (sclarke)
on 2009-05-21 13:34
Hi all,

This is one of those annoying ones, but simple to solve I would imagine.

I have a word list, where each entry is on a single line and in
lowercase. I process this text file reading each word

words.each |word|
etc etc

I then have a conditional statement to check if a value matches word eg

if foo == word
  puts foo
end

This all works well, but I am stuck on how to deal with upper and
lowercase. I want to check if foo is equal to word in lowercase and in
uppercase.

What is the best way to do this?

Many thanks

Stuart
245a6d22816ecaeac1c9080ad183b859?d=identicon&s=25 badboy (Guest)
on 2009-05-21 13:41
(Received via mailing list)
Stuart Clarke schrieb:
> I then have a conditional statement to check if a value matches word eg
>
> Many thanks
>
> Stuart
you can use regexes and the "i"-flag:
   foo =~ /^yourword$/i
  (or better /\Ayourword\Z/i if you're not sure if there are newlines in
it)
or lowercase foo:
   foo.downcase == "yourword"

;)
Cf25fbf53c67e27d95845e77e949b56f?d=identicon&s=25 Stuart Clarke (sclarke)
on 2009-05-21 13:54
Thanks for a quick reply. I had tried the first one but it didn't work.

Did not think of the second one, silly me.

Thanks a lot

badboy wrote:
> Stuart Clarke schrieb:
>> I then have a conditional statement to check if a value matches word eg
>>
>> Many thanks
>>
>> Stuart
> you can use regexes and the "i"-flag:
>    foo =~ /^yourword$/i
>   (or better /\Ayourword\Z/i if you're not sure if there are newlines in
> it)
> or lowercase foo:
>    foo.downcase == "yourword"
>
> ;)
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-05-21 15:45
(Received via mailing list)
On May 21, 2009, at 7:54 AM, Stuart Clarke wrote:
>>> word eg
>>   foo.downcase == "yourword"
>>
>> ;)
>
> --

Or just:

  yourword.casecmp(foo).zero?

See String#casecmp for more.

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.