The latest Sandi Metz book (99 Bottles) is due soon why not join the mailing list. In her recent post, Sandi succesfully tackles the Roman Numeral Kata using refinements to modify String and Fixnum classes. This was only possible because there are actually strict rules governing the legal subtractive form of roman numerals (back in the day they probably stuck to the additive forms anyway, kept the stone masons busy). Ninety-nine as in example below is a case in point where IC for example is illegal (preceding character letters cannot be less than ten times the value of the character they precede). See gist for roman_numerals.rb or sign up to Sandi’s mailing list for the original. I have a feeling that this is the first instance of using refinements in a JRubyArt sketch.

blackboard.rb


# blackboard.rb inspired by madam Sandi
require_relative 'roman_numerals'
attr_reader :f, :ninety_nine, :fifty_seven, :forty_two

ADDFORM = '%s plus %s equals %s'
SUBFORM = '%s minus %s equals %s'
using Roman

def setup
  sketch_title 'Math Blackboard'
  @f = createFont('Arial', 24, true)
  @ninety_nine = 'XCIX'
  @fifty_seven = 'LVII' 
  @forty_two = 'XLII'
end

def draw
  background 10
  text_font(f, 24)
  fill(0, 220, 220)
  text('Roman Math Blackboard JRubyArt', 80, 50)
  text(format(ADDFORM, ninety_nine, fifty_seven, add(ninety_nine, fifty_seven)), 80, 80)
  text(format(ADDFORM, ninety_nine, forty_two, add(ninety_nine, forty_two)), 80, 105)
  text(format(ADDFORM, forty_two, fifty_seven, add(forty_two, fifty_seven)), 80, 130)
  text(format(SUBFORM, ninety_nine, fifty_seven, sub(ninety_nine, fifty_seven)), 80, 155)
  text(format(SUBFORM, ninety_nine, forty_two, sub(ninety_nine, forty_two)), 80, 180)
end

def add(a, b)
  (a.to_i(:roman) + b.to_i(:roman)).to_roman
end

def sub(a, b)
  (a.to_i(:roman) - b.to_i(:roman)).to_roman
end

def settings
  size 690, 240
end