17 August 2008

Code Can Be Poetic

While learning RSpec (a testing framework for Ruby), my girlfriend was sitting next to me on our couch asking me what color of yarn she should knit some socks in. While she asked me questions, I came up with these to snippits of code that "tested" if a particular yarn type was correct. Not very useful, but fun!

sock.rb:
class Sock
attr_accessor :colors

def initialize
@colors = [:teal, :olive, :green, :blue, :purple]
end

def machine_washable?
true
end
end

sock_spec.rb:
require "sock"

describe "Socks that Kevin would like" do

before :each do
@sock = Sock.new
end

it "should not have pink in it" do
@sock.colors.include?(:pink).should_not == true
end

it "should not have purple in it" do
@sock.colors.include?(:purple).should_not == true
end

it "should be machine washable" do
@sock.should be_machine_washable
end

it "should not have grey in it" do
@sock.colors.include?(:grey).should_not == true
end

it "should have multiple colors" do
@sock.colors.length.should > 1
end

end

No comments: