Ruby | Case Statement Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 3 Likes 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. Create Quiz Comment A ankita_saini Follow 3 Improve A ankita_saini Follow 3 Improve Article Tags : Ruby Ruby-Basics Explore OverviewRuby For Beginners3 min readRuby Programming Language (Introduction)4 min readComparison of Java with Other Programming Languages4 min readSimilarities and Differences between Ruby and C language3 min readSimilarities and Differences between Ruby and C++3 min readEnvironment Setup in Ruby3 min readHow to install Ruby on Linux?2 min readHow to install Ruby on Windows?2 min readInteresting facts about Ruby Programming Language2 min readBasicsRuby | Keywords4 min readRuby | Data Types3 min readRuby Basic Syntax3 min readHello World in Ruby2 min readRuby | Types of Variables4 min readGlobal Variable in Ruby2 min readComments in Ruby2 min readRuby | Ranges4 min readRuby Literals4 min readRuby Directories5 min readRuby | Operators11 min readOperator Precedence in Ruby2 min readOperator Overloading in Ruby5 min readRuby | Pre-define Variables & Constants5 min readRuby | unless Statement and unless Modifier2 min readControl StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 13 min readRuby | Loops (for, while, do..while, until)5 min readRuby | Case Statement3 min readRuby | Control Flow Alteration7 min readRuby Break and Next Statement2 min readRuby redo and retry Statement2 min readBEGIN and END Blocks In Ruby2 min readFile Handling in Ruby4 min readMethodsRuby | Methods3 min readMethod Visibility in Ruby3 min readRecursion in Ruby4 min readRuby Hook Methods5 min readRuby | Range Class Methods5 min readThe Initialize Method in Ruby2 min readRuby | Method overriding2 min readRuby Date and Time3 min readOOP ConceptsObject-Oriented Programming in Ruby | Set 19 min readObject Oriented Programming in Ruby | Set-28 min readRuby | Class & Object4 min readPrivate Classes in Ruby3 min readFreezing Objects | Ruby2 min readRuby | Inheritance4 min readPolymorphism in Ruby3 min readRuby | Constructors2 min readRuby | Access Control8 min readRuby | Encapsulation2 min readRuby Mixins3 min readInstance Variables in Ruby3 min readData Abstraction in Ruby3 min readRuby Static Members3 min readExceptionsRuby | Exceptions4 min readRuby | Exception handling6 min readCatch and Throw Exception In Ruby3 min readRaising Exceptions in Ruby4 min readRuby | Exception Handling in Threads | Set - 12 min readRuby | Exception Class and its Methods3 min readRuby RegexRuby | Regular Expressions3 min readRuby Search and Replace2 min readRuby ClassesRuby | Float Class7 min readRuby | Integer Class3 min readRuby | Symbol Class5 min readRuby | Struct Class5 min readRuby | Dir Class and its methods3 min readRuby | MatchData Class4 min readRuby ModuleRuby | Module4 min readRuby | Comparable Module3 min readRuby | Math Module4 min readInclude v/s Extend in Ruby2 min readCollectionsRuby | Arrays4 min readRuby | String Basics5 min readRuby | String Interpolation3 min readRuby | Hashes Basics4 min readRuby | Hash Class12 min readRuby | Blocks6 min read Like