Project Ramon

A learning journey from a Ruby noob perspective

Codersdojo: Word Wrap

coding dojo word_wrap_header_img
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.

The challenge is from the very first exercise at CodingDojo called word wrap and goes like this:

Scenario from

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:

  1. Create a class named Wrapper
  2. Create an instance method named wrap
  3. The wrap instance method takes two arguments, the first argument string represents the text to be formatted and a boundary which is the column number at which the remainder of the string is placed on a new line with a line break \n

I’ve created two specs for this small exercise which look like so:

coding dojo word_wrap_img1

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 wrapper.rb:

coding dojo word_wrap_img2

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.

Here’s a snapshot from The Ruby documentation:
coding dojo word_wrap_img3

The documentation also provides some pretty solid examples of using this method.

coding dojo word_wrap_img4

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 insert.

Here’s what the Ruby documentation says about the insert method.

coding dojo word_wrap_img5

So Ruby’s 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 where 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.

Ruby’s 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.

coding dojo word_wrap_img6

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.

coding dojo word_wrap_img7

And show an example of this program in action:

1. Create the Wrapper class

coding dojo word_wrap_img8

2. Instantiate the class and have the object call the method.

coding dojo word_wrap_img9

Have a great weekend!


Categories: Newbie, Ruby

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s