#reverse doesn't seem to work for me

Actually, I don’t know if the method is the problem, but here’s what
I’ve
got…

a = ‘stressed’
b = ‘desserts’
a == b.reverse
=> true
words = File.open(‘american-english’, ‘r’) {|f| f.read}.split.map {|w|
w.strip.downcase}
words.include? ‘stressed’
=> true
words.include? ‘desserts’
=> true
working = words.select {|w| w.length = 8}
working.include? ‘stressed’
=> true
working.include? ‘desserts’
=> true
palindrome = working.select {|w| w == w.reverse}
palindrome.empty?
=> true

It works for words of other lengths, though.

On May 1, 2014, at 11:10, Todd B. [email protected] wrote:

working = words.select {|w| w.length = 8}
working.include? ‘stressed’
=> true
working.include? ‘desserts’
=> true
palindrome = working.select {|w| w == w.reverse}
palindrome.empty?
=> true

Maybe I’m misunderstanding what you’re trying to do… but stressed and
desserts aren’t palindromes…

You’re right, it’s not a palindrome. I feel like an idiot. I’d need to
do
all the words for a comparison; in other words phrased, “Is there a word
out of the selection that is the reverse of my word.” Yep, wasn’t
thinking.

‘stressed’ != ‘desserts’ so testing that w == w.reverse is working just
fine.

You probably want something like working.include?(w.reverse) although
that’s an ugly concept that I’m sure someone would have a better answer
for.

palindrome = working.select {|w| w == w.reverse}

John

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