string equivalency

B

Brad Tilley

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?
 
R

Robert Klemme

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
 
B

Brad Tilley

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?
 
R

Robert Klemme

Brad said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top