Ruby

Ruby Koans

KoansI decided to tackle a little Ruby language learning this weekend. So yesterday, I downloaded the Ruby Koans and went to work. The Koans were created by EdgeCase to help people learn Ruby. Basically, they are a set of unit tests that exercise aspects of Ruby. To begin, all of the tests fail. You have to write code or jump back and forth to the Interactive Ruby Shell (IRB) and get the results of the statements to make the asserts pass.

I went through and did 105 unit tests yesterday, each of them having several parts to them. Having messed around a little with Rails, I was familiar with using IRB and Rake and a little of the syntax. In my opinion, the way this is set up and builds upon itself is really helpful and is a great way to teach Ruby to n00bs. I really salute Jim Weirich and Joe O’Brien for their work in getting these together.

One of the exercises is to create a triangle method that takes three parameters (the sizes of the sides). You then have to decide what kind of triangle it is. After those tests pass, your next assignment is to prevent 0 length or negative sides as well as making sure your triangle validates.

I wrote all the code and passed all the tests except for the triangle validation test. I was having a serious brain cramp and could not remember how you ensured that you’ve gotten the correct sides for any triangle (and not just a right triangle), so I looked for someone else’s example of this code. I found this code:

# Triangle Project Code.

# Triangle analyzes the lengths of the sides of a triangle
# (represented by a, b and c) and returns the type of triangle.
#
# It returns:
#   :equilateral  if all sides are equal
#   :isosceles    if exactly 2 sides are equal
#   :scalene      if no sides are equal
#
# The tests for this method can be found in
#   about_triangle_project.rb
# and
#   about_triangle_project_2.rb
#
def triangle(a, b, c)
  if a<=0 or b<=0 or c<=0 
    fail TriangleError, "Sides must all have length greater than 0."
  end

  myArray = [a,b,c]
  maxSide = myArray.max
  total = 0
  myArray.each { |myEle| total += myEle }
  if total - maxSide <= maxSide
    fail TriangleError, "Sum of shortest two sides must be greater than length of longest side."
  end

  if a==b and a==c
     :equilateral 
  elsif a!=b and b!=c and a!=c
    :scalene
  else
     :isosceles
  end
end

# Error class used in part 2.  No need to change this code.
class TriangleError < StandardError
end

Okay, I see. The smallest two sides added together need to be greater than the third side. However, I thought that the author of this code greatly over-complicated this process. We are always dealing with a fixed number of sides and the trick is to put them in order and then add away. I figured Ruby had a simple array sort (and they do), so I would place the sides into an array, sort it, and then add the 0 index and 1 index spot and make sure that it was greater than the 2 index spot. Case Closed and (in my opinion) a bit easier to read. Forgiving of course, that I didn’t pass messages in my exceptions, I personally like my code better (of course 😉 ). I also appreciate that Ruby doesn’t mind me putting ( and ) around my if conditions. I just feel safer for them to be contained 😉

# Triangle Project Code.

# Triangle analyzes the lengths of the sides of a triangle
# (represented by a, b and c) and returns the type of triangle.
#
# It returns:
#   :equilateral  if all sides are equal
#   :isosceles    if exactly 2 sides are equal
#   :scalene      if no sides are equal
#
# The tests for this method can be found in
#   about_triangle_project.rb
# and
#   about_triangle_project_2.rb
#
def triangle(a, b, c)
  # WRITE THIS CODE
  if (a <= 0 or b <= 0 or c<=0)
    raise TriangleError
  end
  
  sides = [a, b, c].sort
  
  if (sides[0] + sides[1] <= sides[2])
    raise TriangleError
  end
  
  if (a==b and a==c)
    value = :equilateral
  else
    if (a==b or a==c or b==c)
      value = :isosceles
    else
      value = :scalene
    end    
  end  
end

# Error class used in part 2.  No need to change this code.
class TriangleError < StandardError
end

As I do these Koans, I’ve been making note of things I’d like to include in future blog entries about things I’ve enjoyed about the language and will be posting them in the near term.

Mentoring

The Importance of Mentoring

Photo by Okinawa Soba from his Flickr Account at http://www.flickr.com/photos/24443965@N08/2585609947/If you would allow me a personal moment for a second, I promise I’ll tie it in to something technical and relevant. Recently, my grandfather passed away at age 84. He was a brilliant man in both quantity of knowledge and the application of that knowledge – especially in the realm of finance. I won’t quote his accomplishments here in the interest of time and topic, but I assure you they were many.

As my father and I were handling all of the things that need taken care of following the death of a loved one, I got to thinking about my grandfather and what he knew. I got down a little bit thinking of how his lifetime of knowledge and wisdom has now been lost and humanity was poorer for it. However, upon further reflection, I realized that these things were not lost; they lived on with my father and me.

My grandfather raised my father, teaching him and guiding him along the way. My father in turn raised me with many of those same principles and my grandfather was there as well for help and guidance when called upon. Many of you know where I’m going with this. You knew two paragraphs ago, but I’m going to say it anyway! We need to mentor other developers in this same style.

We need to have Code Children and Code Grandchildren, Code Cousins and Code Siblings. Each developer goes on a journey; learning little by little along the way from books, blogs, user groups, trial and error, forums, etc. However, in my experience, I’ve seen very little “direct mentoring”.

“Direct mentoring” (according to me) is the deliberate pouring out of your knowledge into another person. You take ownership in their learning process; you become an essential piece of their growth. I’m talking about something different than “drive-by” question answering where we offer a little help and move on. True mentoring is a deliberate act. You have chosen a person (and they have accepted you) and you become someone taking an active part in their learning and development. You can suggest books to them, peer review code, answer questions, and be a person who holds them accountable on goals like learning a new language a year or reading a book a month.

I know that I am not covering brand new ground here. I also know that there are other people talking about this very thing. The “developer as a craftsman” group is very into this idea, describing it through parallels to apprenticeship in other vocations. I would like to see developers begin to mentor others and to in turn also seek out a mentor so that we can all learn from each other and give all of our hard earned knowledge some new legs by passing it on to someone else.

Errors

Fun With The iPhone – DFU Mode

I recently applied a firmware update to my iPhone and it broke. The phone wasn’t jailbroken and there was nothing shady about the dealing whatsoever. However, when I applied the 2.2.1 firmware, iTunes announced failure and my phone went into the revovery mode where it just shows the screen below.

iPhone Recovery Mode

I let iTunes attempt to recover my phone to no avail. I finally did some frantic Googling and came across a site called TheBigBoss.org, where I found a post about DFU Mode. I did what The Big Boss said and used the very strange shift+click* on the restore button in iTunes to allow me to choose what backup file went to my iPhone and finally was able to recover my phone.

* Note: Also from The Big Boss, “If you need to restore a specific version of firmware onto your device, rather than click restore, hold shift (PC) or option (MAC) and click restore. This will bring up a browse box where you can select the firmware you wish to program.”

I can’t believe that this had to be such a convoluted process, but I’m thankful to TheBigBoss.org website for all of the wealth of information it contained. Maybe if this blog can help one person deal with this problem just a little quicker than they would have otherwise, it is all worth it.

Errors

Rails Install Issue

So, I am making a serious go of learning Rails. I have installed it on my Vista machine before, but never really given it a go. So, now that I’ve got a Mac, I decided to try to learn Rails on it. I downloaded the Ruby One-Click Installer and installed it. Per its suggestion, I ran

sudo gem install rails

Then, I got this error

Bulk updating Gem source index for: 
http://gems.rubyforge.org ERROR:  
While executing gem ... (Gem::GemNotFoundException)     
Could not find rails (> 0) in any repository

I did some Googling and found out that I had to just run this code

sudo gem update

and select the correct packages to include. Then, I just reran

sudo gem install rails

and I was money in the bank.

I hope this helps anyone else who encounters the same issue. Now, on to working through some tutorials.

Code Tips

Liskov Substitution Principle

I was listening to Hanselminutes a few weeks back and Scott Hanselman had Uncle Bob Martin on to talk about the SOLID principles of object-oriented design. SOLID stands for

  • Single responsibility principle
  • Open closed principle
  • Liskov substitution principle
  • Interface segregation principle
  • Dependency inversion principle

Obviously, each one of those could warrant its own blog post and Robert Martin himself has written and spoken about them extensively. Uncle Bob did bring up a classic problem on Hanselminutes, though, that I wanted to take some time to talk about.

We who design object-oriented systems have a problem. We’ve been taught that the whole world is made of objects and that we are supposed to model our software after the real world. However, as I’ll cover in this post, sometimes that is a mistake.

The Liskov Substitution Principle says that “Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it” (Uncle Bob’s paraphrase). I have found this one somewhat confusing in the past, but I think that this Rectangle-Square problem explains the problem very well.

In math, the definition of a rectangle is “a parallelogram with four right angles”. One Webster’s definition of a square is “a rectangle with all four sides equal”. Right in the definition from the real world, a square is a rectangle. “Is a” is often a key phrase in object-oriented design used to denote an inheritance relationship.

So, let’s pretend we have the following code:

public class Rectangle
    {
        public virtual int Width { get; set; }
        public virtual int Height { get; set; }

        public virtual int ComputeArea()
        {
            return Width * Height;
        }
    }

    public class Square : Rectangle
    {
        private int _height;
        public override int Width 
        {
            get { return _height; }
            set
            {
                _height = value;
            }
        }

        public override int Height
        {
            get { return _height; }
            set
            {
                _height = value;
            }
        }
        
        public override int ComputeArea()
        {
            return base.ComputeArea();
        }
    }

Okay, that works. You can run code against it and at first blush it behaves like it should. However, the Liskov Substitution Principle says that you should be able to have

Rectangle r = new Square();

and have no problems.

While you can do that and can then operate on r as if it were a rectangle, there is a problem. A person who only knows about rectangles might do this to our r.

Rectangle r = someMethodThatWillReturnASquareSometimes();
r.Width = 10;
r.Height = 15;

They would then get entirely unpredictable results when they computed the area or even went back in to retrieve the properties (finding one had changed without their knowledge). A person who would want to operate in a safe way would have to eventually do the following:

Rectangle r = someMethodThatWillReturnASquareSometimes();

if (r is Square)
{
  // Special Square Processing
}
else
{
  // Normal Rectangle Stuff
}

That now pretty much defeats the purpose of using base classes and interfaces. If you have to know about derived classes and their implementation, you’ve lost the battle. I think that this is a great reminder to model objects logically how they affect the program, not how they reflect “real life”.