1 Vudokus

Ruby Case Statement Assignments

I found case statements in Ruby pretty interesting as they are capable of doing a little more than the equivalent constructs in other languages. We all know how a simple case statement works, we test on a condition that we give to a case statement, we then walk through a set of possible matches each of which is contained in a when statement e.g.:

print "Enter your grade: " grade = gets.chomp case grade when "A" puts 'Well done!' when "B" puts 'Try harder!' when "C" puts 'You need help!!!' else puts "You just making it up!" end

So far nothing special, the above works just the way you would expect. But, you can do more with a case statement in Ruby.

Multi-Value When And No-Value Case

Ruby allows you to supply multiple values to a when statement rather than just one e.g.:

print "Enter your grade: " grade = gets.chomp case grade when "A", "B" puts 'You pretty smart!' when "C", "D" puts 'You pretty dumb!!' else puts "You can't even use a computer!" end

Pretty cool, but nothing too revolutionary. It doesn’t end there though you can use a case statement without giving it a value to match against, which allows a case statement to mimic the behavior of an if statement, e.g.:

print "Enter a string: " some_string = gets.chomp case when some_string.match(/\d/) puts 'String has numbers' when some_string.match(/[a-zA-Z]/) puts 'String has letters' else puts 'String has no numbers or letters' end

So, why would you use it instead of an if statement? Well, there is probably no reason, they are equivalent. But, it is good to be aware that you can do this, especially if you run into it in the wild, wouldn’t want to be caught unawares.

It All Evaluates To An Object

You probably keep hearing this over and over, but everything in Ruby works with objects. Things are no different when it comes to case statements. A case statement will always return a single object, just like a method call. So, you can safely wrap a case statement in a method call and Ruby will have no problems with it. For example, the above version of the case statement ca be re-written like this:

print "Enter a string: " some_string = gets.chomp puts case when some_string.match(/\d/) 'String has numbers' when some_string.match(/[a-zA-Z]/) 'String has letters' else 'String has no numbers or letters' end

We are now wrapping the whole case statement in a puts method call, rather than doing it within each individual when statement. This works because no matter which when/else succeeds, the whole case statement returns the result of the last line that was executed (in our case it just always returns a string), and so the puts just writes out whatever string is returned.

How It All Works Under The Hood

You can almost consider case/when statements to be syntactic sugar for a method call. Every object in Ruby inherits a method called the case equality method, also known as the triple equals (===). You can think of it as an operator if it helps (the === operator), but we know that Ruby operators are just syntactic sugar for method calls. So whenever a when is trying to match a value (except when we are using a no-value case) there is a method call behind the scenes to the === method/operator. We can therefore take our very first example and re-write it using if statements to be completely equivalent to a case statement:

print "Enter your grade: " grade = gets.chomp if "A" === grade puts 'Well done!' elsif "B" === grade puts 'Try harder!' elsif "C" === grade puts 'You need help!!!' else puts "You just making it up!" end

The implications of this are as follows. Any class you write can override the === method and therefore define it’s own behavior for usage in a case statement. For built-in objects such as strings, the === operator/method is simply equivalent to the == method/operator. But you don’t have to be restricted to this. If you want your class to have the ability to participate in a case statement in some specialized way all you need to do is something like this:

class Vehicle attr_accessor :number_of_wheels def initialize(number_of_wheels) @number_of_wheels = number_of_wheels end def ===(another_vehicle) self.number_of_wheels == another_vehicle.number_of_wheels end end four_wheeler = Vehicle.new 4 two_wheeler = Vehicle.new 2 print "Enter number of wheel for vehicle: " vehicle = Vehicle.new gets.chomp.to_i puts case vehicle when two_wheeler 'Vehicle has the same number of wheels as a two-wheeler!' when four_wheeler 'Vehicle has the same number of wheels as a four-wheeler!' else "Don't know of a vehicle with that wheel arrangement!" end

In this case even though we are matching directly on the vehicle object in our case/when statement, behind the scenes a method call is made to the === operator and so the real match is made on the number of wheels attribute that the vehicle object has (as per what is defined in the === method).

This idea of hiding method calls behind syntax that doesn’t look like method calls, seems to be rather common in Ruby. Personally I like it, you can do some rather elegant things, but the building blocks are still just a bunch of method calls. When it comes to the case statement though, I hardly ever use it when I program in Java for example, but it looks to me like it can be somewhat more handy in Ruby especially with the ability to define custom case behavior for your own objects.

Image by dahliascakes

Expressions



So far we've been fairly cavalier in our use of expressions in Ruby. After all, is pretty standard stuff. You could write a whole heap of Ruby code without reading any of this chapter. But it wouldn't be as much fun . One of the first differences with Ruby is that anything that can reasonably return a value does: just about everything is an expression. What does this mean in practice? Some obvious things include the ability to chain statements together.
»
»
Perhaps less obvious, things that are normally statements in C or Java are expressions in Ruby. For example, the and statements both return the value of the last expression executed.
songType = if song.mp3Type == MP3::Jazz              if song.written < Date.new(1935, 1, 1)                Song::TradJazz              else                Song::Jazz              end            else              Song::Other            end  rating = case votesCast           when 0...10    then Rating::SkipThisOne           when 10...50   then Rating::CouldDoBetter           else                Rating::Rave           end
We'll talk more about and starting on page 79.

Operator Expressions

Ruby has the basic set of operators (+, -, *, /, and so on) as well as a few surprises. A complete list of the operators, and their precedences, is given in Table 18.4 on page 219. In Ruby, many operators are actually method calls. When you write you're actually asking the object referenced by to execute the method ``'', passing in the parameter . You then ask the object that results from that calculation to execute ``'', passing as a parameter. This is exactly equivalent to writing Because everything is an object, and because you can redefine instance methods, you can always redefine basic arithmetic if you don't like the answers you're getting.
»
»
More useful is the fact that classes that you write can participate in operator expressions just as if they were built-in objects. For example, we might want to be able to extract a number of seconds of music from the middle of a song. We could using the indexing operator ``'' to specify the music to be extracted.
class Song   def [](fromTime, toTime)     result = Song.new(self.title + " [extract]",                       self.artist,                       toTime - fromTime)     result.setStartTime(fromTime)     result   end end
This code fragment extends class with the method ``'', which takes two parameters (a start time and an end time). It returns a new song, with the music clipped to the given interval. We could then play the introduction to a song with code such as:

Miscellaneous Expressions

As well as the obvious operator expressions and method calls, and the (perhaps) less obvious statement expressions (such as and ), Ruby has a few more things that you can use in expressions.

Command Expansion

If you enclose a string in backquotes, or use the delimited form prefixed by , it will (by default) be executed as a command by your underlying operating system. The value of the expression is the standard output of that command. Newlines will not be stripped, so it is likely that the value you get back will have a trailing return or linefeed character.
»
»
»
You can use expression expansion and all the usual escape sequences in the command string.
for i in 0..3   status = `dbmanager status id=#{i}`   # ... end
The exit status of the command is available in the global variable .

Backquotes Are Soft

In the description of the command output expression, we said that the string in backquotes would ``by default'' be executed as a command. In fact, the string is passed to the method called (a single backquote). If you want, you can override this.
alias oldBackquote ` def `(cmd)   result = oldBackquote(cmd)   if $? != 0     raise "Command #{cmd} failed"   end   result end print `date` print `data`
produces:
Sun Jun  9 00:08:26 CDT 2002 prog.rb:3: command not found: data prog.rb:5:in ``': Command data failed (RuntimeError) from prog.rb:10

Assignment

Just about every example we've given so far in this book has featured assignment. Perhaps it's about time we said something about it. An assignment statement sets the variable or attribute on its left side (the lvalue) to refer to the value on the right (the rvalue). It then returns that value as the result of the assignment expression. This means that you can chain assignments and that you can perform assignments in some unexpected places.
»
»
»
»
There are two basic forms of assignment in Ruby. The first assigns an object reference to a variable or constant. This form of assignment is hard-wired into the language.
instrument = "piano" MIDDLE_A   = 440
The second form of assignment involves having an object attribute or element reference on the left-hand side.
aSong.duration    = 234 instrument["ano"] = "ccolo"
These forms are special, because they are implemented by calling methods in the lvalues, which means you can override them. We've already seen how to define a writable object attribute. Simply define a method name ending in an equals sign. This method receives as its parameter the assignment's rvalue.
class Song   def duration=(newDuration)     @duration = newDuration   end end
There is no reason that these attribute setting methods must correspond with internal instance variables, or that there has to be an attribute reader for every attribute writer (or vice versa).
class Amplifier   def volume=(newVolume)     self.leftChannel = self.rightChannel = newVolume   end   # ... end
Sidebar: Using Accessors Within a Class
Why did we write in the example on page 74? Well, there's a hidden gotcha with writable attributes. Normally, methods within a class can invoke other methods in the same class and its superclasses in functional form (that is, with an implicit receiver of ). However, this doesn't work with attribute writers. Ruby sees the assignment and decides that the name on the left must be a local variable, not a method call to an attribute writer.
»
»
We forgot to put ``'' in front of the assignment to , so Ruby stored the new value in a local variable of method ; the object's attribute never got updated. This can be a tricky bug to track down.

Parallel Assignment

During your first week in a programming course (or the second semester if it was a party school), you may have had to write code to swap the values in two variables:
int a = 1; int b = 2; int temp; temp = a; a = b; b = temp;
You can do this much more cleanly in Ruby:
a, b = b, a
Ruby assignments are effectively performed in parallel, so the values assigned are not affected by the assignment itself. The values on the right-hand side are evaluated in the order in which they appear before any assignment is made to variables or attributes on the left. A somewhat contrived example illustrates this. The second line assigns to the variables , , and the values of the expressions , , and , respectively.
»
»
When an assignment has more than one lvalue, the assignment expression returns an array of the rvalues. If an assignment contains more lvalues than rvalues, the excess lvalues are set to . If a multiple assignment contains more rvalues than lvalues, the extra rvalues are ignored. As of Ruby 1.6.2, if an assignment has one lvalue and multiple rvalues, the rvalues are converted to an array and assigned to the lvalue. You can collapse and expand arrays using Ruby's parallel assignment operator. If the last lvalue is preceded by an asterisk, all the remaining rvalues will be collected and assigned to that lvalue as an array. Similarly, if the last rvalue is an array, you can prefix it with an asterisk, which effectively expands it into its constituent values in place. (This is not necessary if the rvalue is the only thing on the right-hand side---the array will be expanded automatically.)
b,  c = a »b == 1,c == 2
b, *c = a »b == 1,c == [2, 3, 4]
b,  c = 99,  a »b == 99,c == [1, 2, 3, 4]
b, *c = 99,  a »b == 99,c == [[1, 2, 3, 4]]
b,  c = 99, *a »b == 99,c == 1
b, *c = 99, *a »b == 99,c == [1, 2, 3, 4]

Nested Assignments

Parallel assignments have one more feature worth mentioning. The left-hand side of an assignment may contain a parenthesized list of terms. Ruby treats these terms as if they were a nested assignment statement. It extracts out the corresponding rvalue, assigning it to the parenthesized terms, before continuing with the higher-level assignment.
b, (c, d), e = 1,2,3,4 »b == 1,c == 2,d == nil,e == 3
b, (c, d), e = [1,2,3,4] »b == 1,c == 2,d == nil,e == 3
b, (c, d), e = 1,[2,3],4 »b == 1,c == 2,d == 3,e == 4
b, (c, d), e = 1,[2,3,4],5 »b == 1,c == 2,d == 3,e == 5
b, (c,*d), e = 1,[2,3,4],5 »b == 1,c == 2,d == [3, 4],e == 5

Other Forms of Assignment

In common with many other languages, Ruby has a syntactic shortcut: may be written as . The second form is converted internally to the first. This means that operators that you have defined as methods in your own classes work as you'd expect.
»
»

Conditional Execution

Ruby has several different mechanisms for conditional execution of code; most of them should feel familiar, and many have some neat twists. Before we get into them, though, we need to spend a short time looking at boolean expressions.

Boolean Expressions

Ruby has a simple definition of truth. Any value that is not or the constant is true. You'll find that the library routines use this fact consistently. For example, , which returns the next line from a file, returns at end of file, enabling you to write loops such as:
while line = gets   # process line end
However, there's a trap here for C, C++, and Perl programmers. The number zero is not interpreted as a false value. Neither is a zero-length string. This can be a tough habit to break.

Defined?, And, Or, and Not

Ruby supports all the standard boolean operators and introduces the new operator . Both ``'' and ``'' evaluate to true only if both operands are true. They evaluate the second operand only if the first is true (this is sometimes known as ``short-circuit evaluation''). The only difference in the two forms is precedence (``'' binds lower than ``''). Similarly, both ``'' and ``'' evaluate to true if either operand is true. They evaluate their second operand only if the first is false. As with ``'', the only difference between ``'' and ``'' is their precedence. Just to make life interesting, ``'' and ``'' have the same precedence, while ``'' has a higher precedence than ``''. ``'' and ``'' return the opposite of their operand (false if the operand is true, and true if the operand is false). And, yes, ``'' and ``'' differ only in precedence. All these precedence rules are summarized in Table 18.4 on page 219. The operator returns if its argument (which can be an arbitrary expression) is not defined, otherwise it returns a description of that argument. If the argument is , returns the string ``yield'' if a code block is associated with the current context.
»
»
»
»
»
»
»
»
»
In addition to the boolean operators, Ruby objects support comparison using the methods , , , , , and (see Table 7.1 on page 79). All but are defined in class but are often overridden by descendents to provide appropriate semantics. For example, class redefines so that two array objects are equal if they have the same number of elements and corresponding elements are equal.
Common comparison operators
OperatorMeaning
Test for equal value.
Used to test equality within a clause of a statement.
General comparison operator. Returns -1, 0, or +1, depending on whether its receiver is less than, equal to, or greater than its argument.
, , , Comparison operators for less than, less than or equal, greater than or equal, and greater than.
Regular expression pattern match.
True if the receiver and argument have both the same type and equal values. 1 == 1.0 returns , but 1.eql?(1.0) is .
True if the receiver and argument have the same object id.
Both and have negated forms, and . However, these are converted by Ruby when your program is read. is equivalent to , and is the same as . This means that if you write a class that overrides or you get a working and for free. But on the flip side, this also means that you cannot define and independent of and , respectively. You can use a Ruby range as a boolean expression. A range such as will evaluate as false until becomes true. The range will then evaluate as true until becomes true. Once this happens, the range resets, ready to fire again. We show some examples of this on page 82. Finally, you can use a bare regular expression as a boolean expression. Ruby expands it to .

If and Unless Expressions

An expression in Ruby is pretty similar to ``if'' statements in other languages.
if aSong.artist == "Gillespie" then   handle = "Dizzy" elsif aSong.artist == "Parker" then   handle = "Bird" else   handle = "unknown" end
If you lay out your statements on multiple lines, you can leave off the keyword.
if aSong.artist == "Gillespie"   handle = "Dizzy" elsif aSong.artist == "Parker"   handle = "Bird" else   handle = "unknown" end
However, if you lay your code out more tightly, the keyword is necessary to separate the boolean expression from the following statements.
if aSong.artist == "Gillespie" then  handle = "Dizzy" elsif aSong.artist == "Parker" then  handle = "Bird" else  handle = "unknown" end
You can have zero or more clauses and an optional clause. As we've said before, is an expression, not a statement---it returns a value. You don't have to use the value of an expression, but it can come in handy.
handle = if aSong.artist == "Gillespie" then            "Dizzy"          elsif aSong.artist == "Parker" then            "Bird"          else            "unknown"          end
Ruby also has a negated form of the statement:
unless aSong.duration > 180 then   cost = .25 else   cost = .35 end
Finally, for the C fans out there, Ruby also supports the C-style conditional expression:
cost = aSong.duration > 180 ? .35 : .25
The conditional expression returns the value of either the expression before or the expression after the colon, depending on whether the boolean expression before the question mark evaluates to or . In this case, if the song duration is greater than 3 minutes, the expression returns .35. For shorter songs, it returns .25. Whatever the result, it is then assigned to .

If and Unless Modifiers

Ruby shares a neat feature with Perl. Statement modifiers let you tack conditional statements onto the end of a normal statement.
mon, day, year = $1, $2, $3 if /(\d\d)-(\d\d)-(\d\d)/ puts "a = #{a}" if fDebug print total unless total == 0
For an modifier, the preceding expression will be evaluated only if the condition is true. works the other way around.
while gets   next if /^#/            # Skip comments   parseLine unless /^$/   # Don't parse empty lines end
Because itself is an expression, you can get really obscure with statements such as:
if artist == "John Coltrane"   artist = "'Trane" end unless nicknames == "no"
This path leads to the gates of madness.

Case Expressions

The Ruby expression is a powerful beast: a multiway on steroids.
case inputLine   when "debug"     dumpDebugInfo     dumpSymbols   when /p\s+(\w+)/     dumpVariable($1)   when "quit", "exit"     exit   else     print "Illegal command: #{inputLine}" end
As with , returns the value of the last expression executed, and you also need a keyword if the expression is on the same line as the condition.
kind = case year          when 1850..1889 then "Blues"          when 1890..1909 then "Ragtime"          when 1910..1929 then "New Orleans Jazz"          when 1930..1939 then "Swing"          when 1940..1950 then "Bebop"          else                 "Jazz"        end
operates by comparing the target (the expression after the keyword ) with each of the comparison expressions after the keywords. This test is done using comparison  target. As long as a class defines meaningful semantics for (and all the built-in classes do), objects of that class can be used in case expressions. For example, regular expressions define as a simple pattern match.
case line   when /title=(.*)/     puts "Title is #$1"   when /track=(.*)/     puts "Track is #$1"   when /artist=(.*)/     puts "Artist is #$1" end
Ruby classes are instances of class , which defines as a test to see if the argument is an instance of the class or one of its superclasses. So (abandoning the benefits of polymorphism and bringing the gods of refactoring down around your ears), you can test the class of objects:
case shape   when Square, Rectangle     # ...   when Circle     # ...   when Triangle     # ...   else     # ... end

Loops

Don't tell anyone, but Ruby has pretty primitive built-in looping constructs. The loop executes its body zero or more times as long as its condition is true. For example, this common idiom reads until the input is exhausted.
while gets   # ... end
There's also a negated form that executes the body until the condition becomes true.
until playList.duration > 60   playList.add(songList.pop) end
As with and , both of the loops can also be used as statement modifiers.
a *= 2 while a < 100 a -= 10 until a < 100
On page 78 in the section on boolean expressions, we said that a range can be used as a kind of flip-flop, returning true when some event happens and then staying true until a second event occurs. This facility is normally used within loops. In the example that follows, we read a text file containing the first ten ordinal numbers (``first,'' ``second,'' and so on) but only print the lines starting with the one that matches ``third'' and ending with the one that matches ``fifth.''
file = File.open("ordinal") while file.gets   print  if /third/ .. /fifth/ end
produces: The elements of a range used in a boolean expression can themselves be expressions. These are evaluated each time the overall boolean expression is evaluated. For example, the following code uses the fact that the variable contains the current input line number to display line numbers one through three and those between a match of and .
file = File.open("ordinal") while file.gets   print if ($. == 1) || /eig/ .. ($. == 3) || /nin/ end
produces:
first second third eighth ninth
There's one wrinkle when and are used as statement modifiers. If the statement they are modifying is a / block, the code in the block will always execute at least one time, regardless of the value of the boolean expression.
print "Hello\n" while false begin   print "Goodbye\n" end while false
produces:

Iterators

If you read the beginning of the previous section, you might have been discouraged. ``Ruby has pretty primitive built-in looping constructs,'' it said. Don't despair, gentle reader, for there's good news. Ruby doesn't need any sophisticated built-in loops, because all the fun stuff is implemented using Ruby iterators. For example, Ruby doesn't have a ``for'' loop---at least not the kind you'd find in C, C++, and Java. Instead, Ruby uses methods defined in various built-in classes to provide equivalent, but less error-prone, functionality. Let's look at some examples.
3.times do   print "Ho! " end
produces: It's easy to avoid fencepost and off-by-1 errors; this loop will execute three times, period. In addition to , integers can loop over specific ranges by calling , , and . For instance, a traditional ``for'' loop that runs from 0 to 9 (something like ) is written as follows.
0.upto(9) do |x|   print x, " " end
produces:
0 1 2 3 4 5 6 7 8 9
A loop from 0 to 12 by 3 can be written as follows.
0.step(12, 3) {|x| print x, " " }
produces:
0 3 6 9 12
Similarly, iterating over arrays and other containers is made easy using their method.
[ 1, 1, 2, 3, 5 ].each {|val| print val, " " }
produces:
1 1 2 3 5
And once a class supports , the additional methods in the module (documented beginning on page 403 and summarized on pages 102--103) become available. For example, the class provides an method, which returns each line of a file in turn. Using the method in , we could iterate over only those lines that meet a certain condition.
File.open("ordinal").grep /d$/ do |line|   print line end
produces: Last, and probably least, is the most basic loop of all. Ruby provides a built-in iterator called .
loop {   # block ... }
The iterator calls the associated block forever (or at least until you break out of the loop, but you'll have to read ahead to find out how to do that).

For ... In

Earlier we said that the only built-in Ruby looping primitives were and . What's this ``'' thing, then? Well, is almost a lump of syntactic sugar. When you write
for aSong in songList   aSong.play end
Ruby translates it into something like:
songList.each do |aSong|   aSong.play end
The only difference between the loop and the form is the scope of local variables that are defined in the body. This is discussed on page 87. You can use to iterate over any object that responds to the method , such as an or a .
for i in ['fee', 'fi', 'fo', 'fum']   print i, " " end for i in 1..3   print i, " " end for i in File.open("ordinal").find_all { |l| l =~ /d$/}   print i.chomp, " " end
produces:
fee fi fo fum 1 2 3 second third
As long as your class defines a sensible method, you can use a loop to traverse it.
class Periods   def each     yield "Classical"     yield "Jazz"     yield "Rock"   end end periods = Periods.new for genre in periods   print genre, " " end
produces:
Classical Jazz Rock

Break, Redo, and Next

The loop control constructs , , and let you alter the normal flow through a loop or iterator. terminates the immediately enclosing loop; control resumes at the statement following the block. repeats the loop from the start, but without reevaluating the condition or fetching the next element (in an iterator). skips to the end of the loop, effectively starting the next iteration.
while gets   next if /^\s*#/   # skip comments   break if /^END/   # stop at end                     # substitute stuff in backticks and try again   redo if gsub!(/`(.*?)`/) { eval($1) }   # process line ... end
These keywords can also be used with any of the iterator-based looping mechanisms:
i=0 loop do   i += 1   next if i < 3   print i   break if i > 4 end
produces:

Retry

The statement causes a loop to repeat the current iteration. Sometimes, though, you need to wind the loop right back to the very beginning. The statement is just the ticket. restarts any kind of iterator loop.
for i in 1..100   print "Now at #{i}. Restart? "   retry if gets =~ /^y/i end
Running this interactively, you might see
Now at 1. Restart? n Now at 2. Restart? y Now at 1. Restart? n  . . .
will reevaluate any arguments to the iterator before restarting it. The online Ruby documentation has the following example of a do-it-yourself until loop.
def doUntil(cond)   yield   retry unless cond end i = 0 doUntil(i > 3) {   print i, " "   i += 1 }
produces:
0 1 2 3 4

Variable Scope and Loops

The , , and loops are built into the language and do not introduce new scope; previously existing locals can be used in the loop, and any new locals created will be available afterward. The blocks used by iterators (such as and ) are a little different. Normally, the local variables created in these blocks are not accessible outside the block.
[ 1, 2, 3 ].each do |x|   y = x + 1 end [ x, y ]
produces:
prog.rb:4: undefined local variable or method `x'
for #<Object:0x401c2ce0> (NameError)
However, if at the time the block executes a local variable already exists with the same name as that of a variable in the block, the existing local variable will be used in the block. Its value will therefore be available after the block finishes. As the following example shows, this applies both to normal variables in the block and to the block's parameters.
»

Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/)). Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder. Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.

Leave a Comment

(0 Comments)

Your email address will not be published. Required fields are marked *