Information Technology Reference
In-Depth Information
case statements also support a default case, in the event that nothing else matches.
For example, you could use Ruby's case statement to classify players on a baseball team
based on their age. Players younger than 12 are considered to be in the minor league.
Players between 13 and 18 are developing. Players between 19 and 30 are in their prime.
Players still in the league between 31 and 40 are on their decline, and anyone over 40 is
in retirement. This logic is captured by the following Ruby case statement:
case
case player . age
when
when 0 . . 12
'Minor League'
when
when 13 . . 18
'Developing'
when
when 19 . . 30
'Prime'
when
when 31 . . 40
'Decline'
else
else
'Retirement'
end
end
Methods, Classes, and Modules
Although not necessary until more advanced interactions with Chef, Ruby also supports
methods, classes, modules, and an object-oriented hierarchy. Ruby methods are defined
using the def keyword. Ruby classes are defined using the class keyword. Ruby mod-
ules are defined using the module keyword:
class
class Bacon
Bacon
def
def cook ( temperature )
# ...
end
end
end
end
module
module Edible
Edible
# ...
end
end
Cl asses are created using the class keyword.
 
 
 
 
Search WWH ::




Custom Search