The easiest rubinius core library patch tutorial ever.

This is a 5 minutes introduction to create a core patch to Rubinius.

  1. Write a core library patch to Rubinius is really easy. First we need to decide what we want to write. Run the specs and find a bug, or write your own specs. Im going to write my own spec. I decide to implement the missing spec hypotenuse form Math Module
  2. A bit of Test driven development :
    1. Find the math specs file : RUBINIUS_HOME/spec/core/math_spec.rb
    2. Write the spec :
      1. create the describe :
        describe "Math#hypot" doend
      2. Add the basic cases :
        it "return x**2+y**2" do      
           Math.hypot(0,0).should_be_close(0.0,TOLERANCE)Math.hypot(2,10).should_be_close( 10.1980390271856 ,TOLERANCE)
           Math.hypot(5000,5000).should_be_close(7071.06781186548 ,TOLERANCE)
      3. Add exceptional cases :
         it "raise an ArgumentError exception if the argument are  the wrong type" do 
         it "raise an TypeError exception if the argument is nill" do  
    3. run the specs. ruby spec/core/math_spec.rb. Umm 3 fails
  3. Ok, lets coding
    1. Find the math module : RUBINIUS_HOME/kernel/core/math.rb
    2. Write the method
      1. def hypot(x,y)
            sqrt(x**2 + y**2); 
      2. rebuild the core: rake build:core
      3. run the test again … ummm two fails. not so bad.
    3. Lets code again :
      1. The method :
          def hypot(x,y)     raise ArgumentError unless x.kind_of? Numeric or y.kind_of? Numericsqrt(x**2 + y**2)end
      2. rebuild the core
      3. re run the specs … 0 fails !!
      4. Thats all.
    4. No !! thats not al. As Tony Targonski point in the comments we have a bug. Ok lets write a case for the bug.

2 comments so far

  1. Tony on

    So what about hypot(42, “life, the universe and everything”) ?

  2. Pedro on

    Thanks for the feedback i update the code.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: