Leon's Blogging

Coding blogging for hackers.

Ruby - Case When

| Comments

Basic

1
2
3
4
5
6
7
8
9
10
num = 8

case num
  when 1, 3, 5, 7, 9
    puts "#{num} is odd"
  when 2, 4, 6, 8, 10
    puts "#{num} is even"
end

# 8 is even
1
2
3
4
5
6
7
8
9
10
num = 8

case num
  when 1..5
    puts "hi"
  when 6..10
    puts "hello"
end

# hello
1
2
3
4
case num
  when 1..5 then puts "hi"
  when 6..10 then puts "hello"
end

case when, checking the class type

1
2
3
4
5
6
7
8
9
a = "Zigor"
case a
when String
  puts "Its a string"
when Fixnum
  puts "Its a number"
end

# Its a string

case when and regular expressions

1
2
3
4
5
6
7
8
9
10
11
string = "I Love Ruby"
# string = "I Love Python"

case string
when /Ruby/
  puts "string contains Ruby"
else
  puts "string does not contain Ruby"
end

# string contains Ruby

case when and lambdas

1
2
3
4
5
6
7
8
9
10
num = 76

case num
when -> (n) { n % 2 == 0 }
  puts "#{num} is even"
else
  puts "#{num} is odd"
end

# 76 is even

Comments