technology from back to front

Archive for November, 2011

Randomly testing Ruby

I recently ran into the need for testing the behaviour of a parser of a modelling language. The parser processes a number of model descriptions in gem files, as well as local definitions. Until recently, the parser would process the gems in an arbitrary order. However the language while ostensibly declarative, isn’t, because of a huge restructuring of the parser. As a result, some bugs lurk in the changed code. These bugs, because of the arbitrary processing order of the model gems, manifest on some machines and not others. Forcing an ordering on the gem processing masks the underlying issues, even while letting the users of the parser to get on with their lives.

What to do? Random testing to the rescue! I cast around for Ruby ports of QuickCheck, and found two: rushcheck and rantly. Rushcheck hasn’t been wrapped up in a gem, so I decided to take rantly for a spin.

Read more…

by
Frank Shearar
on
26/11/11

King Kong! Misadventures in Ruby meta-programming

Sometimes after a particularly fraught bug stomping session you make a frivolous offhand remark to a colleague, for example “I will write a macro that converts lisp definitions in prefix form so that arithmetic looks like how it was taught to you in school” or “I won’t let my unit test really be an integration test ever again”. Today after wrestling with Ruby I mentioned a mild dislike for monkey patching as often used in Ruby and the idea for King Kong was born.

If you don’t like monkey patching in Ruby, then King Kong is the solution. He is a very large primate who likes to have his own way. If you define a class and then monkey patch it King Kong will eat it, it will be gone, no longer available for your use. Fortunately King Kong can’t use source code control systems (yet!) so you can correct your error and ponder his subtle ways.

We shall start by writing a spec:

require "rspec"
require "king-kong"

class X
  def x()
     "x"
 end
end

describe "King Kong" do

   it "does not allow monkey patching" do
        Object.const_defined?("X").should == true

     class X
         def x()
             "not x"
         end
     end

     Object.const_defined?("X").should == false
  end
end

Now we define King Kong!

class Class
    @@method_history = {}

   def method_added(method_name)
       @@method_history[self] ||= {}
       if @@method_history[self][method_name] then
         puts "King kong is the top primate\nHe has eaten your class!"
          Object.send(:remove_const, self.name)
       else
                @@method_history[self][method_name] = true
      end
 end
end

Run the spec, spec passes, job done! This code is licensed as is and probably shouldn’t be run in production, or even your QA environment but you are welcome to run it on your development machine for minimal comedic effect.

King Kong is a fine example of Ruby meta-programming and an excellent addition to your arsenal of offensive programming tools taking a large amount of inspiration from tools like Guantanamo. Marvel at how easy it is to completely annihilate a class from existence in Ruby, if that was Java you would have to give a class loader or something a walloping with a GreasySpannerFactory to achieve the same thing.

Remember sometimes monkey patching is a solution and some times it is the cause of the problem!

If you want to take a more serious approach to this problem you might want to look here.

by
tim
on
16/11/11

8-bit style acrylic sculpture

A while back I did a post talking about working with MDF and a laser cutter. In that, I was using a program to generate the design for a sculpture and feeding the design to a laser cutter. This time around, the design is fairly simple and doesn’t need much computer help to build it, but there’s a few different options for how things can look, and so I’d like to be able to preview it beforehand. Previously, I was using Blender, but that’s a bit heavyweight for my little netbook, and given I’m just using it for viewing rather than editing, there are better options, and so I’m having a look at one of those along the way as well.

My tool of choice for today is the Python Computer Graphics Kit a.k.a. cgkit (I’ve put together a Debian packaged version while doing this). The documentation is a bit variable, but they’ve got a nice little viewer program that we can hack away at and use to make a tool to do what we want.
Read more…

by
Tom Parker
on
06/11/11

Search

Categories

You are currently browsing the LShift Ltd. blog archives for November, 2011.

Feeds

Archives

2000-14 LShift Ltd, 1st Floor, Hoxton Point, 6 Rufus Street, London, N1 6PE, UK+44 (0)20 7729 7060   Contact us