Ruby | Case Statement Last Updated : 11 Jul, 2025 Suggest changes Share Like Article Like Report The case statement is a multiway branch statement just like a switch statement in other languages. It provides an easy way to forward execution to different parts of code based on the value of the expression. There are 3 important keywords which are used in the case statement: case: It is similar to the switch keyword in another programming languages. It takes the variables that will be used by when keyword. when: It is similar to the case keyword in another programming languages. It is used to match a single condition. There can be multiple when statements into a single case statement. else: It is similar to the default keyword in another programming languages. It is optional and will execute when nothing matches. Syntax: case expression when expression 1 # your code when expression 2 # your code . . else # your code end Flow Chart: Example 1: Ruby # Ruby program to illustrate the # concept of case statement #!/usr/bin/ruby print "Input from one, two, three, four: " # taking input from user # str = gets.chomp # hardcoded input str = "two" # using case statement case str # using when when "one" puts 'Input is 1' when "two" puts 'Input is 2' when "three" puts 'Input is 3' when "four" puts 'Input is 4' else puts "Default!" end Output: Input from one, two, three, four: Input is 2 Example 2: Ruby # Ruby program to illustrate # case statement #!/usr/bin/ruby marks = 70 # marks is the input # for case statement case marks # using range operators .. when 0..32 puts "You fail!" when 33..40 puts "You got C grade!" when 41..60 puts "You got B grade!" else puts "You got A grade!" end Output: You got A grade! Important Points: In case statement the when statement can contain multiple values and range(see above example). Example: Ruby # Ruby program to illustrate # how to use multiple values # in when statement choice = "5" # using 'case' statement case choice # here 'when' statement contains # the two values when "1","2" puts "You order Espresso!" when "3","4" puts "You order Short Macchiato!" when "5","6" puts "You order Ristretto!" when "7","8" puts "You order Cappuccino!" else "No Order!" end Output: You order Ristretto! You can also use case statement without any value. Example: Ruby # Ruby program to illustrate no # value in case statement str = "GeeksforGeeks" # here case statement # has no value case # using match keyword to check when str.match(/\d/) puts 'String contains numbers' when str.match(/[a-zA-Z]/) puts 'String contains letters' else puts 'String does not contain numbers & letters' end Output: String contains letters You can use case statement in method call. Like method call, a case statement will always return a single object. Example: Ruby # Ruby program to illustrate case # statement in a method call str = "1234" # here case statement # has no value & used as # in puts method call puts case # using match keyword to check when str.match(/\d/) 'String contains numbers' when str.match(/[a-zA-Z]/) 'String contains letters' else 'String does not contain numbers & letters' end Output: String contains numbers Explanation: Here we are using the case statement in a puts method call. The benefit of doing this that we can omit the puts from the when statement. A ankita_saini Follow Article Tags : Ruby Ruby-Basics Explore Ruby Programming Language 4 min read OverviewRuby For Beginners 3 min read Ruby Programming Language (Introduction) 4 min read Comparison of Java with Other Programming Languages 4 min read Similarities and Differences between Ruby and C language 3 min read Similarities and Differences between Ruby and C++ 3 min read Environment Setup in Ruby 3 min read How to install Ruby on Linux? 2 min read How to install Ruby on Windows? 2 min read Interesting facts about Ruby Programming Language 2 min read BasicsRuby | Keywords 4 min read Ruby | Data Types 3 min read Ruby Basic Syntax 3 min read Hello World in Ruby 2 min read Ruby | Types of Variables 4 min read Global Variable in Ruby 2 min read Comments in Ruby 2 min read Ruby | Ranges 4 min read Ruby Literals 4 min read Ruby Directories 5 min read Ruby | Operators 11 min read Operator Precedence in Ruby 2 min read Operator Overloading in Ruby 5 min read Ruby | Pre-define Variables & Constants 5 min read Ruby | unless Statement and unless Modifier 2 min read Control StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 1 3 min read Ruby | Loops (for, while, do..while, until) 5 min read Ruby | Case Statement 3 min read Ruby | Control Flow Alteration 7 min read Ruby Break and Next Statement 2 min read Ruby redo and retry Statement 2 min read BEGIN and END Blocks In Ruby 2 min read File Handling in Ruby 4 min read MethodsRuby | Methods 3 min read Method Visibility in Ruby 3 min read Recursion in Ruby 4 min read Ruby Hook Methods 5 min read Ruby | Range Class Methods 5 min read The Initialize Method in Ruby 2 min read Ruby | Method overriding 2 min read Ruby Date and Time 3 min read OOP ConceptsObject-Oriented Programming in Ruby | Set 1 9 min read Object Oriented Programming in Ruby | Set-2 8 min read Ruby | Class & Object 4 min read Private Classes in Ruby 3 min read Freezing Objects | Ruby 2 min read Ruby | Inheritance 4 min read Polymorphism in Ruby 3 min read Ruby | Constructors 2 min read Ruby | Access Control 8 min read Ruby | Encapsulation 2 min read Ruby Mixins 3 min read Instance Variables in Ruby 3 min read Data Abstraction in Ruby 3 min read Ruby Static Members 3 min read ExceptionsRuby | Exceptions 4 min read Ruby | Exception handling 6 min read Catch and Throw Exception In Ruby 3 min read Raising Exceptions in Ruby 4 min read Ruby | Exception Handling in Threads | Set - 1 2 min read Ruby | Exception Class and its Methods 3 min read Ruby RegexRuby | Regular Expressions 3 min read Ruby Search and Replace 2 min read Ruby ClassesRuby | Float Class 7 min read Ruby | Integer Class 3 min read Ruby | Symbol Class 5 min read Ruby | Struct Class 5 min read Ruby | Dir Class and its methods 3 min read Ruby | MatchData Class 4 min read Ruby ModuleRuby | Module 4 min read Ruby | Comparable Module 3 min read Ruby | Math Module 4 min read Include v/s Extend in Ruby 2 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like