FAQ: The Zen of Ruby - When and Then: The Case Statement

This community-built FAQ covers the “When and Then: The Case Statement” exercise from the lesson “The Zen of Ruby”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise When and Then: The Case Statement

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

When Then are so cool & easy haha! Love it!

2 Likes

Ok so why did my code work but not be accepted as correct? This is what I put?

puts “What Language?”
greeting = gets.chomp

Add your case statement below!

case greeting
when “English”
puts “Hello!”
when “French”
puts “Bonjour!”
when “German”
puts “Guten Tag!”
when “Finnish”
puts “Haloo!”
else
puts “I don’t know that language!”
end

As mentioned, the program worked but wouldn’t let me move forward. The exercise did say I could use either the format above or what the solution put below

case greeting
when “English” then puts “Hello!”
when “French” then puts “Bonjour!”
when “German” then puts “Guten Tag!”
when “Finnish” then puts “Haloo!”
else puts “I don’t know that language!”
end

1 Like

Same thing I got and it did not let me continue

1 Like

The Ruby track is exhibiting signs of an unstable environment in which user inputs become an issue. The workaround is to define the input variables with a literal value and let the code run its course.

For this exercise, we could benefit from an array of possible inputs and go through the list.

inputs = ['English', 'French', 'German', 'Finnish', 'Hindi']

The last one is added to test the default case.

I’m hoping by this point some of the built in iterators have already been introduced.

inputs.each do |greeting| 
    case greeting
    # ...
    end
end
1 Like

Does anyone know why the code with == doesn’t work?

The code below doesn’t work:

case greeting
  when greeting == "English" then puts "Hello!"
  else puts "I don't know that language!"
end

But this one works fine:

case greeting
  when greeting = "English" then puts "Hello!"
  else puts "I don't know that language!"
end```

case will pair up its expression with the when that it matches…

case greeting
  when "English" ...
  when "French" ...
  when "German" ...
  when "Finnish" ...
  else ...
end

This course seems to be ordered in a strange way. Case and when were introduced earlier in the course, but this lesson seems to introduce them as if they are new concepts. I suggest either removing any repeats or making this entire zen lesson a sort of review (unless there are new concepts introduced in it, which seems unlikely).

I’d like to note that the then keyword was not introduced earlier in the other lesson.

2 Likes

Hi!
“Add a case greeting statement (in either style!) to the editor.”

What would be the other styles for the case statement?

I couldn’t find any other style related to it, would it be “===”?

Editing: just found out that it might be the then keyword, still not sure if that’s it :smiley:

1 Like

I agree, I think it’s the “then” keyword as in the example Codecademy shows all the “then” keywords are in light blue, but when typed into the script.rb, the “then” keywords are shown red, not light blue.

My code below (which I think is correct but to move onto the next topic I probably can’t used the below code, aka, the “then” keyword):

Not a big deal. It could be from the older learning environment/editor, which has changed periodically on the site. The code makes sense, and the syntax highlighting is consistent so don’t read any more into it.

1 Like

Yeah, I think so, too! I’m over this problem, but always fun to visit with other coders and talk about potential issues and how to go around it! :slight_smile:

1 Like

Lesson worked ok for me.
My solution strives to be pretty & readable:

ruby code
puts "Hello there!"
greeting = gets.chomp
puts case greeting
  when "English"  then  "Hello!"
  when "French"   then  "Bonjour!"
  when "German"   then  "Guten Tag!"
  when "Finnish"  then  "Haloo!"
  else "I don't know that language!"
end

For those struggling in this exercise, this worked for me:

greeting = []
puts "Hello there!"
greeting = gets.chomp
greeting.downcase
# Add your case statement below!
case greeting
  when "english" then puts "Hello!"
  when "french" then puts "Bonjour!"
  when "german" then puts "Guten Tag!"
  when "finnish" then puts "Haloo!"
  else
  puts "I don't know that language!"
end

this is my code, identical to what i know its suppose to be and still it does’nt let me progress

puts “Hello there!”
greeting = gets.chomp

Add your case statement below!

case greeting
when “English” then puts “Hello!”
when “French” then puts “Bonjour!”
when “German” then puts “Guten Tag!”
when “Finnish” then puts “Haloo!”
else puts “I don’t know that language!”
end

Is it accepting your input and then printing the appropriate response? If not, set greeting manually and run the program.

Hi there,

thank you so much for replying to me. I’m not quite sure what you mean to setting greeting manually? Nothing is printing in my console and the solution is the exact same as what I’ve put. Really apprecite you helping, I’ve been stuck on this for hours. This is what prints

Hello there!
execution expired

1 Like

When you click Run, be sure to click where the prompt is in the console and quickly enter your text and press Enter. There is only a short time window before that input prompt times out.

By manual setting, we assign the literal value rather that gets.chomp.

greeting = "English"
2 Likes

wow it worked! Thank you so much! So if this situation happens again, do I always have to apply the literal value rather than gets.chomp? If so how comes?

We cannot judge the situation as always, just as a fallback if the input (gets.chomp) expires too quickly, or won’t take input at all. It at least lets us see the code run through to completion.