Categories
Technology

Learn By Do. Part I

If you want to learn to code, the best way is to code… lots. No getting around it, and for me, my fingers seem to remember more than my brain. So keeping that in mind, here begins a short series of code snippets and samples which i’ve used to help myself learn the Ruby language. Hopefully, this helps you out a little as well as you start exploring the language for yourself. Of course, a lot of the information is also gleaned from around the web. I’ll endeavour to give credit where it’s due, if i forget or leave anyone out, it’s not intentional. Please feel free to prompt me a kind “ahem”. And also, i ain’t no matter expert, so if you spot an inefficiency somewhere, i’d appreciate a little pointer too. Thank you.

So, the key things that i want to learn are:
* language syntax
* keeping with lessons learned in oop
* how to write/test logic

And then of course, i also want to write something relatively “useful”. Not some imaginary and contrived Product-Catalogue relationship or Animal, Dog, Cat hierarchy, thank you very much.

That’s it, really. So essentially i want to capitalize on the two main languages i’ve spent a lot time in and what they taught me: C++ => OO principles, and then C++/C# => TDD. Armed with the basics, i, and by extension that is, you too, can pretty much tackle just about any problem within my (your) comprehension.

So the first challenge is writing a class that can handle finding the mean, median, variance and standard deviation for an array of numbers (integers or floats). Fits all my criteria and will certainly help me determine and check answers while i study through my courses at uni. That is to say, it’s very useful for me. So, with further ado…

TDD

require ‘test/unit’ at the top of your file and inherit from Test::Unit::TestCase.
If you’re familiar with TDD in general and been exposed to xUnit frameworks, that’s about all you need to know, the rest is straightforward.
So i want to test my new class SampleData which is initialized with an array of numbers and sports three basic methods: mean, median and variance (for now). The first thing i did was create TestSampleData

class TestSampleData < Test::Unit::TestCaseend

First task; make sure you can’t declare SampleData.new without passing in an array of numbers; that is, i want to force usage to always supply an array of numbers when you construct it. Hey, it’s my design so i get to choose the why.

class SampleData
private
  def initialize()
  end
public
  def initialize(elements)
  end
end

Almost like C++, the public and private sections of the class definition are straightforward. Nothing tricky there. Now i’m forced to, at minimum declare instances with a parameter: SampleData.new([]). It’s what i wanted.

Next concern was making sure the parameter passed in is what i expect it to be (defensive programming). Make sure it’s an array of either Fixnum or Float. If it’s not, i want the constructor to raise an exception.
The exception is the easy bit:

raise ArgumentError.new("message")

. Checking the type of the parameter is also easy with the class method.

  def initialize(elements)
    if Array != elements.class
      raise ArgumentError.new("Can only initialize sample data with an array of floating points or integers")
    end
  end

And then checking the type of each element in the array also turns out to be pretty straightforward

    elements.each do |e|
      if (!(Fixnum == e.class) || (Float == e.class))
        raise ArgumentError.new("Array can only contain floating points or integers: item " + e.to_s + " is a " + e.class.to_s)
      end
    end

Of course, all this is verified with a bunch of tests declared by the test case:

  def test_must_init_with_array
    assert_raise(ArgumentError) { SampleData.new("string") }
  end
  def test_must_init_with_array_of_numbers
    assert_raise(ArgumentError) { SampleData.new(['ll', 'llkj']) }
    assert_raise(ArgumentError) { SampleData.new([0, 8, 'll', 9, 7, 'llkj']) }
  end

The code reads for itself. All i need to do to verify is, from the commandline, run ruby filename.rb.

[email protected]:~/src/sandbox/ruby/medians$ ruby sample_data.rb
Loaded suite sample_data
Started
......
Finished in 0.002402 seconds.

6 tests, 16 assertions, 0 failures, 0 errors

That’ll do it for Part I. Key things learned:
* public, private sections of a class definition and the syntax thereof
* writing a test case
* raising exceptions, and testing for expected exceptions
* checking types and forcing class usage
* a little tight looping and boolean logic

So far so good, and really nothing too different or difficult to grasp coming from either C++/C#. Part II to follow shortly…

One reply on “Learn By Do. Part I”

Comments are closed.