Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,109 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