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?

On 16.08.2006 15:42, Brad T. wrote:

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

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?

Brad T. wrote:

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

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…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs