Using your favorite text editor, open split.rb and take a moment to look over its contents, customize its opening documentation, etc.
Like other object-oriented languages,
subprograms in Ruby are called methods.
Since everything in Ruby is an object,
method invocations are messages that are sent to objects.
Let's look at a BNF for a Ruby method:
<MethodDef> ::= 'def' <identifier> <ParameterDecs> <StatementList> 'end' ;
<ParameterDecs> ::= '(' <ParameterDec> <MoreParameters> ')' |
<ParameterDec> <MoreParameters> | ∅ ;
<ParameterDec> ::= <identifier> <DefaultArg> ;
<DefaultArg> ::= = <Expression> | ∅ ;
<MoreParameters> ::= ',' <ParameterDec> <MoreParameters> | ∅ ;
Ruby parameters are not designated to accept a certain type by the syntax; instead, they are assigned a type dynamically according to the arguments passed when the message is sent.
In addition to the standard parameters, Ruby provides two additional parameter types for special situations: the group parameter and the block parameter. Both of these parameters appear at the end of the standard parameter list.
The group parameter is designated by a variable name prefixed by a * symbol. It allows any additional parameters to be collected into an array designated by the variable name. The additional parameters can then be accessed via the array. To illustrate this, here's an example taken from Programming Ruby:
def varargs(arg1, *rest)
puts "Got #{arg1} and #{rest.join(', ')}"
end
varargs("one") #Produces: Got one and
varargs("one", "two") #Produces: Got one and two
varargs "one", "two", "three" #Produces: Got one and two, three
The block parameter is designated by a variable name prefixed by
a & symbol.
It allows any block
that follows the function call to be converted to a Proc
object and manipulated within the method.
We'll examine these a bit later in the course.
Ruby has no reference parameters in the sense of C++, so we will have our method return an array containing the two substrings. To return values from a method, recall that Ruby automatically returns the value of the last statement executed, so we just have to end our method with a statement whose value is an array containing the two substrings we want to return. No return statement is needed (but you can use one if you really want to).
As it happens, Ruby's String class provides a split() method. However, this method "tokenizes" a string, splitting it into substrings based on a delimiter, so it won't solve our problem; to do that, we'll write our own split() function and pass it the String we want to split.
Begin by defining a stub for the split() function, and give it two parameters: aString and position.
In order to retrieve the two substrings, we can use the [] method the String class provides. This method accepts a variety of different arguments that you can read about here. Experiment with those arguments until you find a combination that produces the desired behavior. (Hint: I used two numbers to retrieve the first part and a range to retrieve the second part.) You can use the expression aString.size to find the length of a Ruby string.
In order to pass back the two substrings, we'll put them into an array. Since Ruby lets us use [] to construct array literals, and since Ruby returns the last statement evaluated, we should be able to write the entire body of split() on one line! See if you can do it.
Be sure to test your program on a variety of input values,
as we have done with our other languages.
When you are confident your method is correct, use script to create a recording in which you list your program and show that it works correctly using the input string hello and the index values 0, 3, and 5. Save the resulting script file to be concatenated to your submission.
That concludes the Ruby part of this lab.
If this was your last exercise, return to the
lab 06 page and follow the
"Turn In" instructions there.
See Also Project 6
Calvin > CS > 214 > Labs > 06 > Ruby