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 trialAlphonse Cuccurullo
2,513 PointsCan someone help me with my case statement?
print " Hello there please enter your first name"
puts " if you have the chosen characteristics then we will continue asking questions." name = gets.chomp case name when name.length == 8 puts "thats just the right number of characters" when name <= 8 puts" not enough characters" when name > 8 puts "way to much" else name.to_i puts "fuck you thats not a name" end
3 Answers
Maximiliane Quel
Courses Plus Student 55,489 PointsThere are a number of issues with your code. Since you are declaring a variable, you are not putting its name after the case keyword. You did not include a test for your second case, where you are saying that it is not enough characters.
The else clause in your statement can never happen because
gets stands for get string and therefore you will always get a string even if the user entered 12345678
what you are asking is if the string is either 8, smaller than 8 or greater than 8. that is all the options there are.
If you want to avoid someone putting in, for example, a number instead of letters, you need to check whether the input matches a regular expression.
print " Hello there. Please, enter your first name. "
puts "You will get my appreciation, if you have the chosen characteristics."
entered_name = gets.chomp
case
when entered_name.length == 8 && entered_name.match(/[a-zA-Z]/)
puts "Your name has the perfect number of characters"
when entered_name.length < 8 && entered_name.match(/[a-zA-Z]/)
puts "Your name is way too short."
when entered_name.length > 8 && entered_name.match(/[a-zA-Z]/)
puts "That's way too long a name!"
else
puts "Fuck you. That's not a name!"
end
Whatever the person enters, they will not be asked again. To do that, you would have to create a loop. However, either a person has 8 or does not have 8 letters in their name. So asking them again does not seem like a useful thing to do ;0)
Is that what you were looking for?
Alphonse Cuccurullo
2,513 PointsInteresting mind telling me what you prefer statement wise? An is there a particular need for them given certain problems coding wise? Like do you prefer the if statements or the case statements? Also mind explaining the .match(/[a-zA-Z]/) metod \for me?
Maximiliane Quel
Courses Plus Student 55,489 PointsWhile you could use either, I think in the example above a case statement is a good choice.
Under the hood case statements use a different comparison operator that does not only check for value but also for object equality. This means that there could conceivably be a scenario you can construct where the case statement doesn't work. Again in this example it does not matter and both will work (haven't seen an actual use case where it mattered). I don't often come across examples where I have a lot of conditions that I want to test for and most of the time get away with just using if. Maybe just flip a coin? ;0)
.match is way simpler to explain. /[a-zA-Z]/ is a regular expression, i.e. a pattern that you are searching. / / is the literal you use to represent it, the two / surrounds the actual pattern, if you will. In this case a letter from a to z and A to Z (i.e. a letter no matter if lower of upper case) will be part of the pattern, and the square brackets indicate that is any number of those letters. match is a string method. If you give it a regular expression as a parameter, it will compare the content of your variable with that regular expression and if it does not find a match it will return nil.
Maximiliane Quel
Courses Plus Student 55,489 PointsMaximiliane Quel
Courses Plus Student 55,489 Points@nicjmuller Your answer was a good start. You don't have to be shy ;0)
If you do not want to check that the person enters anything else then letters, then there are indeed only three options and you can use an if else statement.
You need to include a test for the first two conditions, where the name is exactly eight characters and then one for either greater or smaller than. You do not need a second elseif because if it is neither of the first two then it is automatically else.
It would look like this:
I guess the usual check you would want to make is that the person's name has at least two characters (certain James Bond characters would be out of luck) and maybe no more than, uhm 40(? just guessing) letters:
You probably still want to add tests for letters and make it case insensitive.