String equivalency

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-forum.com/.

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

···

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?

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-forum.com/\.

I don't believe it would matter in the case of a String. But it is
important to understand the difference. == compares value. eql? compares
value and type. In the case of ints versus floats for example 1==1.0 is
true, but 1.eql?(1.0) is false.

For some classes though, you will see that .eql? is really just syntactic
sugar sending to == anyway...

···

On 8/16/06, Brad Tilley <rtilley@vt.edu> 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?

--
Posted via http://www.ruby-forum.com/\.

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