Friday, May 23, 2008
I have a small fantasy football related project that I'm going to be working on that requires access to the full 2008 NFL Schedule.  I decided to use Hpricot, and scrape the schedule from ESPN.  The CSV file I created with the output from the below script can be found here: nfl-schedule.csv (11.48 KB)  

Download script (parse_schedule.rb)

#!ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'

class Game
   attr_accessor :date, :week, :away_team, :home_team, :time

   def to_s
      "#{@date} #{@time} #{@away_team} at #{@home_team}"
   end

   def to_csv
      "#{@week},#{@date.gsub(",", "")},#{@time},#{@away_team},#{@home_team}"
   end
end

def parse_games(doc)
   games = []
   doc.search("//table[@class='tablehead']//tr").each do |tr|
      @week = tr.search("/td/a").inner_html if(tr[:class] == 'stathead')
      @date = tr.at("td").inner_html if(tr[:class] == 'colhead')

      teams = []
      tr.at("td").search("a").each do |team|
         teams << team.inner_html
      end

      if(teams.size == 2)
         @time = tr.search("td:eq(1)").inner_html
         game = Game.new()
         game.date = @date
         game.week = @week
         game.time = @time
         game.away_team = teams[0]
         game.home_team = teams[1]
         games << game
      end
   end
   games
end

games = parse_games(Hpricot(open("http://sports.espn.go.com/nfl/schedule")))
games.each do |g|
   puts g.to_csv
end
puts "Total games: #{games.size}"
Saturday, May 24, 2008 12:35:58 AM (Eastern Daylight Time, UTC-04:00) | Comments [12] | #
Monday, May 19, 2008
Have you ever been shamboozled by the WinForms designer in Visual Studio?  If you haven't, its likely only because you've never used it.  Over the last several years I've had many wars with the WinForms designer, and unfortunately I've lost every single time. 

Not long ago as I was exploring a couple interesting open source ruby projects, I came across one that had me longing for something as simple in the .NET world.  The project was shoes, done by none other then why the lucky stiff.  For those unfamiliar with shoes, it's website describes it as:
Shoes is a very informal graphics and windowing toolkit. It's for making regular old apps that run on Windows, Mac OS X and Linux. It's a blend of my favorite things from the Web, some Ruby style, and a sprinkling of cross-platform widgets.
To give you an idea of how simple shoes makes creating the simplest of GUI's checkout the following code:
Shoes.app {
button("Press Me") { alert("You pressed me") }
}
To give you a little more background, shoes uses a ruby "dsl" for describing the layout of desktop GUI, and under the covers is implemented in C.  Although I wasn't sure how far I could get, I decided to "implement" shoes using IronRuby for my philly code camp presentation.  My goal was to keep the same ruby syntax for declaring the layout and controls that make up the screen,  however, instead of using the backend implementation of shoes I wanted to write my own implementation in IronRuby by leveraging the interop capabilities between IronRuby and Windows Forms.  After hacking on it for a few hours a couple nights last week, as well as doing a little more this past weekend I've been able to get enough of shoes replicated in IronRuby to create some reasonable apps.  Let's start with a simple "Hello Iron Shoes" example. 
require "shoes"
Shoes.app :width => 450, :height => 400, :title => "Hello!" do
banner "Hello Iron Shoes"
end

We start with a require statement that includes the shoes.rb file that implements the shoes "dsl".  Every shoes app starts with a call to Shoes.app that takes a hash of options.  In this example we specify the width and height of the form as well as a title.  We then pass a block that contains a simple banner "Hello Iron Shoes" statement.  Within shoes, all layout and nesting is done via Ruby blocks.  So everything within the do...end block of Shoes.app is added as content to the form.  When we run the above using IronRuby we get the following result.

What's you might find particularly interesting is that since Mono just recently completed their implementation of Windows Forms, and since IronRuby is open source we get the same cross platform support from our IronRuby implementation as we get with the real shoes.  We can also add a bit of user interaction by using an edit_line and button like so:

require "shoes"
Shoes.app :width => 200, :height => 200, :title => "Hello!" do
@input = edit_line
button("Say Hello") { alert("Hello #{@input.text}")}
end


Finally to see some of the other controls that are supported checkout this very beautifully designed form:

The code for this beauty is as follows:
require "shoes"
Shoes.app :width => 800, :height => 600, :title => "IronShoes everything sample" do
flow :width => 800, :height => 600 do
stack :width => 300, :height => 600 do
link "CLICK ME" do; alert "HEY"; end
link "GOOGLE", :click => "http://google.com"
image "shoes-logo.png", :click => "http://code.whytheluckystiff.net/shoes/"
@e = edit_line
banner "Hello Iron Shoes"
para "This is shoes...", strong("and another"), em("and a final one")
button "Foo" do
alert("Hello Foo")
end
button "Bar" do
alert("Hello Bar")
end
button "Get Textbox Value" do
alert(@e.text)
end
edit_box :width => 200, :height => 100
end
    stack :width => 400, :height => 600 do
  banner "Banner"
title "Title"
subtitle "Subtitle"
tagline "Tagline"
caption "Caption"
para "This is some more text"
image "rails.png"
para "Jimmy kissed", strong("Margaret"),
"and I've saved a picture somewhere on",
link("Flickr", :click => "http://flickr.com")
end
end
end

To checkout the code for my IronRuby implementation of shoes you can download the zip file containing my demos from my Code Camp presentation.

Overall, the experience of creating a basic implementation of shoes with IronRuby was very enjoyable.  While I did run into some problems, for the most part things went very smoothly.  I created a basic calculator, a very ugly friendfeed client (since twitter was down), as well as the other basic examples shown here. 

There is still a lot of API's available within shoes I'm not handling, as well as many things I'm not handling as well as I could so I'll likely continue to hack around on it a bit.  WPF and Silverlight support would be nice (and very doable) as well!

Tuesday, May 20, 2008 1:24:59 AM (Eastern Daylight Time, UTC-04:00) | Comments [9] | #
This past weekend I got a chance to present on IronRuby at the Philly Code Camp.  I had a really great time preparing and giving this talk, despite more then a few hiccups along the way (keynote trouble and computer crash!).  After getting to talk to a few people afterwards it seems it was well received despite my stumblings.

My primary goal for the presentation was to encourage those in the audience who don't know Ruby to learn it.  I think Ruby is a great programming language that has a ton of really compelling use cases.  Additionally, I think the process of learning Ruby is extremely beneficial and can change the way you think about and write software.

I'm very excited about where the IronRuby project is, and even more so about where it's headed.  The team is making great strides and getting really close to a couple milestones that will be very exciting.  We really need to get as many individuals within the .NET community involved in the IronRuby project as possible so we can have a kick ass implementation of Ruby that runs on the CLR!

I've attached my slides to this post, but be forewarned that they're not all that useful standalone.  I've also attached my sample code/demos.  I had the most fun putting together my "shoes" sample which I'm going to write a follow up post on shortly I've written about in my Creating Cross platform GUI's with IronRuby post.

Slides
Sample Code

Tuesday, May 20, 2008 12:52:26 AM (Eastern Daylight Time, UTC-04:00) | Comments [3] | #
Thursday, May 08, 2008
In preparation for my upcoming Code Camp session on IronRuby I've been hacking around on the IronRuby source, as well as on Ruby programs that can run on IronRuby.  One of the core areas of interest for me concerning IronRuby is in writing specifications for my .NET applications using Ruby.  While I've been learning Ruby (the real kind) I've become very fond of the testing libraries they have available, most notably RSpec and mocha

Over the last two nights I've been writing some ruby code to test .NET classes written in C#.  With a few of the hacks I have locally, I've had some pretty good success.  The following set of specifications verify the behavior of an Account class I've written.

require "../../trunk/tests/ironruby/Util/simple_test.rb"
require "IronRubySamples"
include IronRubySamples

describe "Account" do
    describe "When depositing money into my account" do
        it "should increase my balance by the amount deposited" do
            account = Account.new(10)
            account.Deposit(10)
            account.Balance.should == 20
        end
    end
                               
    describe "When withdrawing money from my account" do
        it "should decrease my balance by the amount withdrawn" do
            account = Account.new(100)
            account.Withdraw(50)
            account.Balance.should == 50
        end
    end
                   
    describe "When withdrawing money from an account with insufficient funds" do
        it "should tell me I have insufficient funds" do
            account = Account.new(30)
            should_raise(InsufficentFundsException) { account.Withdraw(50) }
        end
    end
   
    describe "When making a withdraw that drops my balance below the minimum" do
        it "should reduce my account by the amount withdrawn + the low funds charge amount" do
            account = Account.new(55)
            account.Withdraw(50)
            account.Balance.should == 4
        end
    end
end

The next step in my quest to test .NET code with IronRuby required me to figure out how to mock out dependent objects.  I considered several different options.  My first thought was to try and use "simple mock", however, I quickly realized via Scott Bellware that while it worked well on IronRuby libraries it wouldn't fit my needs.  My next thought was to give Moq a try.  After downloading Moq, I attempted to run a spec that referenced Moq and created a Mock<T> instance as shown below:

require "../../trunk/tests/ironruby/Util/simple_test.rb"
require "IronRubySamples"
require "Moq"
include Moq
include IronRubySamples

describe "LoginController" do
    describe "When a user logs in" do
        it "should vallidate credentials with login service" do
            mock = Mock.of(ILoginService).new
            #do expects
           
            controller = LoginController.new(mock.Object)
            controller.Login("steve", "****")
        end
    end
end

When running this via ir.exe I got an error that "Moq.Mock is not a generic type".  After poking around a bit I discovered this was due to a bug in IronRuby.  Currently IronRuby has problems when there is a generic and non generic type of the same name within a referenced assembly.  In order to work around this, I first tried to figure out what needed to be modified in IronRuby, however, that wasn't very fruitful so I decided to modify the source for Moq to get around my problem.  After renaming the static Mock class in Moq to MockRetriever, and hacking around another bug in IronRuby related to creating generic types where the type argument is an interface, I was finally able to get IronRuby to create the Mock<ILoginService> type:

mock = Mock.of(ILoginService).new

Unfortunately, this led to me to another roadblock.  When setting up expectations in Moq you do so with lambda expressions such as:

// C#
var mock = new Mock<ILoginService>();
mock.Expect(s => s.Login("steve", "****"));

While IronRuby has blocks and lambda's it doesn't have a way to express the above (at least that I know).  I'm going to dig around in the IronRuby source a bit to see if any ideas pop into my head, but at this point I'm not very hopeful. 

Friday, May 09, 2008 1:09:46 AM (Eastern Daylight Time, UTC-04:00) | Comments [3] |  |  | #
Search
Archive
Links
Categories
Admin Login
Sign In
Blogroll
Themes
Pick a theme: