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

14 August 2008

Programming at a High Level

Jeff Atwood (of the Coding Horror blog) this week pointed out that there are many new high-level JavaScript libraries that allow you to ignore the lower-level JavaScript incompatibilities between browsers. I have used JQuery and can also vouch that using JQuery is incredibly fun and easy to program with.

This is more broadly applicable to other programming languages and libraries, too: Use the highest-level interface that you are able to use to accomplish your task. For example, don't use C when you can use C++. Don't use JavaScript when you can use JQuery. Don't use C++ when you can use C#. Don't use Perl when you can use Ruby.

References:
  1. Secrets of the Scripting Ninjas (Coding Horror blog post)

13 August 2008

Ruby Compiler Project

In my spare time (that is, apart from all the other hobbies I have), I have started work on a new programming project. My goal is to write an interpreter/compiler for a new programming language. I have no great ambitions, this project is purely for my own enjoyment and to hopefully teach myself something new.

Goals/Plans:
  1. Develop in Mac OS X
  2. Implement the compiler in Ruby
  3. Use test driven development and the agile process