Well, this is all that’s left for learning the core of the language. How to use ruby’s built in Unit Testing framework and how to fiddle with the debugger. This post will be very short, because there’s not a whole lot to unit testing. Going over the debugger is kinda silly too, since it’s mostly just a command reference. Also, I found an interesting gotcha in the examples listed that I’ll share with you. There’s a benchmarking and profiling module worth saying a few words about.

Unit testing in ruby is just like unit testing in any other language. Assert_* methods abound.

Using the debugger is similar to using gdb or some other debugger as well. There’s a list of commands, you can set breakpoints and watch variables. Nothing very surprising there.

The author offers up a “checklist” for going through your program to ensure that it all works. He firstly reccomends that you run the program with -w. Keep warnings on, so you can resolve issues. He mentions an interesting issue with precendence regarding {} and do…end. To quote the example source code from the book:

def one(arg)
  if block_given?
    "block given to 'one' returns #{yield}"
  else
    arg
  end
end
def two
  if block_given?
    "block given to 'two' returns #{yield}"
  end
end
result1 = one two {
  "three"
}
result2 = one two do
  "three"
end
puts "With braces, result = #{result1}"
puts "With do/end, result = #{result2}"

Which produces:

With braces, result = block given to 'two' returns three
With do/end, result = block given to 'one' returns three

So that’s something to keep in mind. Another gotcha that caught me, is that output may be buffered. I was writing a program to theif images from a website, and I wanted to use print “.” to show completion of an image, but the software wouldn’t show anything. puts “.” would work, but that put a newline character. I hacked around it by calling $stdout.flush to force it to put the . to the console. Setting “sync” on the thing may have worked better.

Also of note is a benchmark module built into ruby. You can profile and run benchmarks and it’ll provide you pretty statistics after running. I’ve used the java profiler before in the past, and it can really help you identify slow parts of your software.

That’s mostly the core of the language, and how to build programs. Now, I get into more of the grunt work of building ruby software. I’ll have to look into section two and see if I’m going to jump directly into rails or not…

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2011 Shlrm.org Blag Suffusion theme by Sayontan Sinha