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 Ruby Operators and Control Structures Ruby Control Structures The Ruby Else Statement

Avi Weiss
Avi Weiss
1,015 Points

.downcase? on word with upper and lowercase?

In this video it is explained to use .downcase so that if someone types in the words using uppercase letters, it will still return true.

What is the proper way to code if the word has both upper and lower case letters?

Example code 1 : print "Would you like to see your name Backwareds? (yes or no)" answer = gets.chomp.downcase

if answer == "yes" puts "this is your name backwards:" puts name.reverse

Example code 2: print "Enter name: " name = gets.chomp

if name == "Avi" puts "Cool! thats my name, too!" else puts "Hi #{name}!" end Example: response in workspaces: Enter name: avi
Hi AVI!

in example 2 it pulls the else statement instead of the if.

5 Answers

Mike Wittenauer
Mike Wittenauer
17,095 Points

.downcase will work fine in this situation and is standard. I will replace all uppercase letters with lowercase and leave the letters that are already lowercase the way they are.

Avi Weiss
Avi Weiss
1,015 Points

Thank you. However, when I tried that, It only seems to pull the else statement.

Code: print "Enter name: " name = gets.chomp.downcase

if name == "Avi" puts "Cool! thats my name, too!" else puts "Hi #{name}!" end

Workspace:
treehouse:~/workspace$ ruby control_structures.rb
Enter name: avi
Hi avi!

Should I change the "if name" ?

Mike Wittenauer
Mike Wittenauer
17,095 Points

That's because when you call .downcase on gets.chomp it is returning "avi". "avi" does not == "Avi" which is why the else statement is running. the == comparison operator is case sensitive.

Mike Wittenauer
Mike Wittenauer
17,095 Points

No problem! Hope that helped :)