Conditional structures/Ruby: Difference between revisions

From Rosetta Code
Content added Content deleted
imported>Katsumi
No edit summary
 
Line 2: Line 2:


===if-then-else===
===if-then-else===
<syntaxhighlight lang="ruby">
<lang ruby>if s == 'Hello World'
if s == 'Hello World'
foo
foo
elsif s == 'Bye World'
elsif s == 'Bye World'
Line 8: Line 9:
else
else
deus_ex
deus_ex
end</lang>
end
</syntaxhighlight>


Note that <code>if...end</code> is an expression, so its return value can be captured in a variable:
Note that <code>if...end</code> is an expression, so its return value can be captured in a variable:


<lang ruby>
<syntaxhighlight lang="ruby">

s = 'yawn'
s = 'yawn'
result = if s == 'Hello World'
result = if s == 'Hello World'
Line 21: Line 24:
:deus_ex
:deus_ex
end
end
# result now holds the symbol :deus_ex</lang>
# result now holds the symbol :deus_ex
</syntaxhighlight>


===ternary===
===ternary===
<syntaxhighlight lang="ruby">
<lang ruby> s == 'Hello World' ? foo : bar</lang>
s == 'Hello World' ? foo : bar
</syntaxhighlight>


===case-when-else===
===case-when-else===
A generic case statement
A generic case statement
<lang ruby>case
<syntaxhighlight lang="ruby">
case
when Time.now.wday == 5
when Time.now.wday == 5
puts "TGIF"
puts "TGIF"
Line 35: Line 42:
else
else
puts "nothing special here"
puts "nothing special here"
end</lang>
end
</syntaxhighlight>
or, comparing to a specific object
or, comparing to a specific object
<syntaxhighlight lang="ruby">
<lang ruby>case cartoon_character
case cartoon_character
when 'Tom'
when 'Tom'
chase
chase
when 'Jerry'
when 'Jerry'
flee
flee
end</lang>
end
</syntaxhighlight>


For the second case, the comparisions are preformed using the <code>===</code> "case equality" method like this: <code>'Tom' === cartoon_character</code>. The default behaviour of <code>===</code> is simple <code>Object#==</code> but some classes define it differently. For example the Module class (parent class of Class) defines <code>===</code> to return true if the class of the target is the specified class or a descendant:
For the second case, the comparisions are preformed using the <code>===</code> "case equality" method like this: <code>'Tom' === cartoon_character</code>. The default behaviour of <code>===</code> is simple <code>Object#==</code> but some classes define it differently. For example the Module class (parent class of Class) defines <code>===</code> to return true if the class of the target is the specified class or a descendant:
<lang ruby>case some_object
<syntaxhighlight lang="ruby">
case some_object
when Numeric
when Numeric
puts "I'm a number. My absolute value is #{some_object.abs}"
puts "I'm a number. My absolute value is #{some_object.abs}"
Line 54: Line 65:
else
else
puts "I'm a #{some_object.class}"
puts "I'm a #{some_object.class}"
end</lang>
end
</syntaxhighlight>


The class Regexp aliases <code>===</code> to <code>=~</code> so you can write a case block to match against some regexes
The class Regexp aliases <code>===</code> to <code>=~</code> so you can write a case block to match against some regexes
<lang ruby>case astring
<syntaxhighlight lang="ruby">
case astring
when /\A\Z/ then puts "Empty"
when /\A\Z/ then puts "Empty"
when /\A[[:lower:]]+\Z/ then puts "Lower case"
when /\A[[:lower:]]+\Z/ then puts "Lower case"
when /\A[[:upper:]]+\Z/ then puts "Upper case"
when /\A[[:upper:]]+\Z/ then puts "Upper case"
else then puts "Mixed case or not purely alphabetic"
else then puts "Mixed case or not purely alphabetic"
end</lang>
end
</syntaxhighlight>


The class Range aliases <code>===</code> to <code>include?</code>:
The class Range aliases <code>===</code> to <code>include?</code>:
<lang ruby>case 79
<syntaxhighlight lang="ruby">
case 79
when 1..50 then puts "low"
when 1..50 then puts "low"
when 51..75 then puts "medium"
when 51..75 then puts "medium"
when 76..100 then puts "high"
when 76..100 then puts "high"
end</lang>
end
</syntaxhighlight>

Latest revision as of 11:18, 20 August 2023

Conditional structures/Ruby is part of Conditional Structures. You may find other members of Conditional Structures at Category:Conditional Structures.

if-then-else

if s == 'Hello World'
  foo
elsif s == 'Bye World'
  bar
else
  deus_ex
end

Note that if...end is an expression, so its return value can be captured in a variable:

s = 'yawn'
result = if s == 'Hello World'
           :foo
         elsif s == 'Bye World'
           :bar
         else
           :deus_ex
         end
# result now holds the symbol :deus_ex

ternary

 s == 'Hello World' ? foo : bar

case-when-else

A generic case statement

case
when Time.now.wday == 5 
  puts "TGIF"
when rand(3) == 2
  puts "had a 33% chance of being right"
else
  puts "nothing special here"
end

or, comparing to a specific object

case cartoon_character
when 'Tom'
  chase
when 'Jerry'
  flee
end

For the second case, the comparisions are preformed using the === "case equality" method like this: 'Tom' === cartoon_character. The default behaviour of === is simple Object#== but some classes define it differently. For example the Module class (parent class of Class) defines === to return true if the class of the target is the specified class or a descendant:

case some_object
when Numeric
  puts "I'm a number.  My absolute value is #{some_object.abs}"
when Array
  puts "I'm an array.  My length is #{some_object.length}"
when String
  puts "I'm a string.  When I'm down I look like this: #{some_object.downcase}"
else
  puts "I'm a #{some_object.class}"
end

The class Regexp aliases === to =~ so you can write a case block to match against some regexes

case astring
when /\A\Z/             then  puts "Empty"
when /\A[[:lower:]]+\Z/ then  puts "Lower case"
when /\A[[:upper:]]+\Z/ then  puts "Upper case"
else                    then  puts "Mixed case or not purely alphabetic"
end

The class Range aliases === to include?:

case 79
when 1..50   then   puts "low"
when 51..75  then   puts "medium"
when 76..100 then   puts "high"
end