Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Hossam Khalifa
Hossam Khalifa
17,200 Points

Ruby String equality

I created a method that dose some basic encryption,changing every letter in a string to next letter. The method works but when I try to compare the string that is returned by the method to the string that should be returned it returns false.

here is the code:

def caesar_cipher(string,int)
  chars = string.chars
 int.times do
   chars.each do |char|
     if ("a".."z")  === char
     char.next!()
    end
  end
 end
  puts chars.join
  chars.join
end

x =  caesar_cipher('What a string!', 5)
puts x.length == "Bmfy f xywnsl!".length #=> true
puts x ==  "Bmfy f xywnsl!" #=> false   =>WHY??

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

So x is actually equal to Wmfy f xywnsl!, and so when you change the B to W it will return true.

Hossam Khalifa
Hossam Khalifa
17,200 Points

did not even notice :) :D