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.

bryan@noah:~/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…

Categories
Technology

Will it Scale?

When Ruby/Rails is mentioned as a web development platform, the most common response is related to scalability and performance; usually with a liberal dash of skepticism. Fear, uncertainty and doubt, along with a couple of honest posts and reviews, all point to the perception that Rails has some sort of performance problem.

300 million page views per month anyone?

Good to hear about the success stories, no matter the tech, but particularly heartening to see the RoR tech maturing into something more than just a “toy”.

Categories
Technology

Open Source on Launchpad

A synergistic moment if there ever was one.

I love my OS. I don’t dislike my XP. It’s been good to me (and continues to be) but i *really* enjoy working on my ubuntu. And i’m still discovering things about it, which is half the fun right there. And as a result, i was introduced to launchpad.

And i really enjoy working on launchpad. I don’t dislike sourceforge. It’s been good to (and continues to be) but i *really* enjoy working on launchpad. And i’m still discovering things about it, which is half the fun right there. (Codeplex hasn’t really shaken my boots much in any form or manner yet).

I love programming. I don’t dislike .NET. It’s been good to (and continues to be) but i *really* enjoy working with Ruby, and in particular, with Rails. And i’m still discovering things about it, which is half the fun right there. And in all my discovery and learning, it was about time to bring the 3 together, in a practical way which helps me be more productive, first and foremost in keeping a tab on my new adventure as an entrepreneur.

Introducing… timesheet. Nothing new, nothing too fancy. It’s Rails, developed on ubuntu, using *nix editors and IDEs where i could scour them, and hosted on launchpad. It works, it’s functional (and i can even port it sans changes to my XP environment) and readily available. Probably has enough bugs (certainly more roadmap expected) to keep me busy exploring features in launchpad, but at least it looks marginally better than my excel spreadsheet (lol)

timesheet.png

Categories
Technology

Ruby Nuby Catch

I was reading up quite a bit and getting a project on the go with Ruby and Streamlined. Perfect for an admin console and mass data capture application. But in my zeal and learning curve, i kept bumping my head against habtm relationships not really working. It was acting weird, and depending on when you pick up on the glitch, you’d explain it a dozen different ways.

Short story long, i scoured- found folk with similar issues and put it down to “bug”- but not convincingly.

And then i was reminded: habtm tables don’t need a surrogate primary key for ActiveRecord development! Doh! changed my associative table scripts to include :id => false and it all works like a charm!

Now if i can only find those threads again and post the “solution” there… Arrrrgg..

But here’s a fantastic post/tutorial on the issue by Sam Aaron.