Begin by using your favorite text editor to edit the file year_codes.rb. Take a moment to study it, to see how it implements our basic algorithm, and compare it to the other "programs" you've seen.
Lets begin by defining a Ruby function with a BNF:
FunctionDef ::= def identifier ParameterDecs StatementList end ParameterDecs ::= (ParameterDec MoreParameters) | ParameterDec MoreParameters | ∅ ParameterDec ::= identifier DefaultArg DefaultArg ::= = Expression | Ø
If you study this BNF closely you'll recognize several unusual things about Ruby methods and parameters:
Using this information, write a method stub called yearCode with a parameter called year.
You can test your stub from the command-line by entering:ruby year_codes.rbIf your stub is syntactically correct, it should run and produce behavior something like this:
Enter the year: freshman Numeric code is: nilMake sure your stub is syntactically correct before you continue.
Now that you've got a method stub written, let's add the selection statement that should run our algorithm. As you probably expected, selection in Ruby takes the form of an if statement. The BNF for a Ruby if statement is as follows:
IfStmt ::= if Expression ThenStmt StatementList
ElsifPart
ElsePart
end
ElsifPart ::= elsif Expression ThenStmt StatementList | ∅
ElsePart ::= else ThenStmt StatementList | ∅
ThenStmt ::= then | ∅
Note: then is required when the if,
elsif, or else appears on the same line as the
StatementList.
One important thing to note about Ruby if statements is that the expression they evaluate is pretty wide open. Similar to the C family of languages, Ruby can evaluate things other than boolean expressions. In Ruby, everything is true except for false and nil. What this means is that any object you throw in there will evaluate to true. Furthermore Ruby has an interesting syntactical twist on the if statement called the unless statement. unless will execute its StatementList unless its Expression is true. The BNF for unless looks like this:
UnlessStmt ::= unless Expression ThenStmt StatementList
ElsePart
end
ElsePart ::= else ThenStmt StatementList | ∅
ThenStmt ::= then | ∅
Note: There is no elsif in an
unless statement.
Another cool feature of selection statements that Ruby borrowed from PERL is the ability to tack them onto the end of an expression. So you could say something like: user.destroy if user.invalid?
One last feature of Ruby we'll need to cover in order to implement our algorithm is how to do string comparison. Ruby provides a variety of ways, including:
We could solve this problem using the standard == operator, though doing so requires us to replace our simple call to gets with (gets).chomp or (gets).strip, because gets includes the newline character when it reads an entered string. (If you want, feel free to try this out before continuing.)
Instead, let's explore a different to solve the problem. Like PERL, Ruby has a fully functional built-in regular expression parser. Regular expressions in Ruby are built with the standard PERL syntax: a valid expression, surrounded by / characters. In order to compare a string in Ruby to a regular expression, we use the =~ operator. Put together, this results in the following statement to determine if the input year matches "freshman":
if year =~ /freshman/
As your implement our algorithm, remember that Ruby methods return the last expression evaluated, so no return statement is necessary. Use all that you've learned about Ruby functions and selection statements to write the algorithm for our year code program.
Be sure to test your program on both valid and invalid input values.
What happens if you enter supersenior as a test-value? Is this a feature or a bug?
When you are confident that your program is working properly, use script to create a recording in which you
That concludes the Ruby part of this lab.
Calvin > CS > 214 > Labs > 03 > Ruby