Today I wanted to cover one of the first kata challenges I completed a while back. Mostly to provide my mind a refresher so that I can reconvene with these exercises. In the future I hope to get more comfortable with decomposing problem sets and strengthen my TDD skill-set.
Scenario from codingdojo.org:
You write a class called Wrapper, that has a single static function named wrap that takes two arguments, a string, and a column number. The function returns the string, but with line breaks inserted at just the right places to make sure that no line is longer than the column number. You try to break lines at word boundaries.
Like a word processor, break the line by replacing the last space in a line with a newline.
I written my interpretation of the instructions from the above problem-set like so:
- Create a class named Wrapper
- Create an instance method named wrap
- The wrap instance method takes two arguments, the first argument
stringrepresents the text to be formatted and a
boundarywhich is the column number at which the remainder of the string is placed on a new line with a line break
I’ve created two specs for this small exercise which look like so:
The first spec on line:4 in the above illustration checks that all objects are instances of the Wrapper class. The second spec is for the
wrap instance method, and ensures that it receives two arguments. I am not confident that I’ve done a great job in covering the behaviors of this program and welcome any suggestions in editing or adding to these specs to make my test coverage more solid.
And here is the program file
So my thoughts on creating this method were as follows; I decided that I would want to separate an incoming string argument into substrings contained inside of an array. Ruby has an instance method named
split that does just that.
The documentation also provides some pretty solid examples of using this method.
My next hurdle was to somehow insert a new line character
\n at a specific place inside of our local variable
string which is now an array containing each of the words as separate values. Ruby has a method for that as well called
Here’s what the Ruby documentation says about the
insert which is also an instance method of the
String class will insert a string at a given index. In this program, boundary is the index number to insert our feature in, and
"\n" is the new line character called for in the project’s problem set. Line 6 reads like this, insert a new line character at
x is the integer argument captured by the boundary variable name.
And now that we have separated our single string into an array of substring values, and created functionality to insert a new line character at whatever boundary passed in as an index, we just need to turn this array back into a single string, now that we are done formatting the string like we see in line 6.
Array class contains an instance method named
join that returns a string created by converting each array element into one string. There is a little more to this method as displayed in the documentation. Here’s an illustration of using dashes
- to separate our condensed array values within one string.
For this project, I just separated by a space
string.join(" ") instead of the dash used in the documentation
string.join("-"), this allows the formatting to visually match the requirement. One string but formatted with the word wrap.
In closing I wanted to demonstrate that the two specs I’ve written passes.
And show an example of this program in action:
1. Create the Wrapper class
2. Instantiate the class and have the object call the method.
Have a great weekend!