[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

string equivalency

Brad Tilley

8/16/2006 1:42:00 PM

What's the best way to check if a string is not exactly what you're
expecting? I'm doing this:

b = 'brad'
if not b.eql?('bradsdhsh')
puts 'not equal'
else
puts 'equal'
end

Is there a more proper way to do it?

--
Posted via http://www.ruby-....

3 Answers

Robert Klemme

8/16/2006 1:46:00 PM

0

On 16.08.2006 15:42, Brad Tilley wrote:
> What's the best way to check if a string is not exactly what you're
> expecting? I'm doing this:
>
> b = 'brad'
> if not b.eql?('bradsdhsh')
> puts 'not equal'
> else
> puts 'equal'
> end
>
> Is there a more proper way to do it?

KISS:

b = 'brad'

if 'bradsdhsh' == b
puts 'equal'
else
puts 'not equal'
end

Or, as a one liner

puts 'bradsdhsh' == b ? 'equal' : 'not equal'

HTH

robert

Brad Tilley

8/16/2006 2:20:00 PM

0

> b = 'brad'
>
> if 'bradsdhsh' == b
> puts 'equal'
> else
> puts 'not equal'
> end
>
> Or, as a one liner
>
> puts 'bradsdhsh' == b ? 'equal' : 'not equal'

This is prefered over the String.eql? method?

--
Posted via http://www.ruby-....

Robert Klemme

8/16/2006 8:43:00 PM

0

Brad Tilley wrote:
>> b = 'brad'
>>
>> if 'bradsdhsh' == b
>> puts 'equal'
>> else
>> puts 'not equal'
>> end
>>
>> Or, as a one liner
>>
>> puts 'bradsdhsh' == b ? 'equal' : 'not equal'
>
> This is prefered over the String.eql? method?

I prefer it and it seems most others, too. Looks better - and note that
it's not like in Java where there is a significant difference between ==
and equals().

Kind regards

robert