Skip to content

Instantly share code, notes, and snippets.

@zmalltalker
Created May 4, 2009 11:12
Show Gist options
  • Save zmalltalker/106423 to your computer and use it in GitHub Desktop.
Save zmalltalker/106423 to your computer and use it in GitHub Desktop.
require 'test/unit'
class Size
include Comparable
attr_reader :size
SIZE_LIST = %w(xxs xs s m l xl xxl)
def self.method_missing(m,*args,&blk)
if SIZE_LIST.include?(m.to_s)
return new(m.to_s)
else
super
end
end
def initialize(s)
@size = s.downcase
end
def <=>(another)
return SIZE_LIST.index(size) <=> SIZE_LIST.index(another.size)
end
end
class SizeTest < Test::Unit::TestCase
def test_comparable
xl = Size.new('XL')
l = Size.new('L')
m = Size.new('M')
assert xl > l
assert l > m
assert xl > m
m2 = Size.new('m')
assert m2 == m
end
def test_magic_methods
xl = Size.xxl
l = Size.xl
assert xl > l
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment