Ruby Articles

Is Ruby on Windows failing when dealing with IPv6?

Posted at 16:10 on Monday, 12 July 2010 — with 1 comment.

RUBY Today, I ran across a potentially show-stopping bug — or perhaps non-feature — in Ruby for Windows and it seems to be (although I don’t believe it) down to Ruby’s inability to handle IPv6.

I presented a question on StackOverflow about it, but thought I’d mirror it here just in case anyone has any ideas. So, without further ado:

So I’ve got a tool that I built in Ruby that uses net/http to make some requests to an external REST service. I built and unit tested this tool using Windows 7 and it worked absolutely fine.

Now, since this tool is meant to be run periodically on one of our servers (running Windows Server 2008 R2), I deployed it there and suddenly it’s failing with the following exception:

SocketError: getaddrinfo: The storage control blocks were destroyed.
  C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `initialize'
  C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `open'
  C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `connect'
  C:/Ruby187/lib/ruby/1.8/timeout.rb:53:in `timeout'
  C:/Ruby187/lib/ruby/1.8/timeout.rb:93:in `timeout'
  C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `connect'
  C:/Ruby187/lib/ruby/1.8/net/http.rb:553:in `do_start'
  C:/Ruby187/lib/ruby/1.8/net/http.rb:542:in `start'
  C:/Ruby187/lib/ruby/1.8/net/http.rb:1035:in `request'
  C:/Ruby187/lib/ruby/1.8/net/http.rb:772:in `get'
    ...

A bit of Googling reveals that this may be an issue with Ruby failing when dealing with IPv6 on a Windows machine. However: it works fine on this Windows 7 machine which has the full IPv6 stack enabled, is connected to the same network and even has the exact same installation of Ruby as the 2K8 server that fails with that error message.

The following is the simplest test case that fails (errors out with the above exception) on Windows Server 2008 but passes on Windows 7:

require "test/unit"
require "net/http"

class IPV6OnWindowsTest < Test::Unit::TestCase

  def test_ipv6_connection
    http = Net::HTTP.new('w3.org', 80)
    response, result = http.get("/", nil)
    assert_not_nil result
  end

end

Remember, using identical Ruby 1.8.7 installations — also occurs with 1.8.6 and 1.9.1, by the way.

Is there anyone out there that can suggest what I might be doing wrong here?

Since these two machines share a lot of properties yet one passes and one fails, I’m finding it hard to believe the conventional wisdom which attributes this to Ruby’s inability to handle IPv6 on Windows.

Thanks in advance!

Tagged as: windows, ruby, ipv6, error, bug

Ruby Game of Life in WPF

Posted at 11:28 on Thursday, 08 July 2010

RUBY A new Ruby Challenge for Newbies has been issued over at RubyLearning by Elise Huard — whom I had the pleasure of hearing talk about evaluating the quality of Rails applications at the Scottish Ruby Conference earlier this year. This challenge, called The Game of Life involves implementing John Conway’s famous Game of Life automaton in Ruby. To explain what it is, let’s quote the Wikipedia article:

The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead. Every cell interacts with its eight neighbours, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

  • Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  • Any live cell with more than three live neighbours dies, as if by overcrowding.
  • Any live cell with two or three live neighbours lives on to the next generation.
  • Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths happen simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the one before). The rules continue to be applied repeatedly to create further generations.

So, of course, I started typing. It is up to the entrant to decide whether they are going to produce a torus-model (where ‘neighbour’ cells for those on the edge of the board wrap around to the other side of the board) or a box-model (whereby edge cells have no neighbours on those sides). I opted for the former. As I was working on this during a lunchbreak at work, I had no access to a UNIX-based machine on which to run the ncurses simulation packaged with the challenge. So, like any programmer worth his salt, I decided to (after spending an hour or two creating an actual working solution, and unit testing the hell out of it) create a visualisation using WPF — written in IronRuby, so it’s still compliant with the challenge!

All my code for this is available at my fork of the challenge on GitHub, but here’s the important part; the WPF code. Isn’t IronRuby awesome?

require 'WindowsBase'
require 'PresentationFramework'
require 'PresentationCore'
require 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

class GameOfLifeSimulation
  include System::Windows::Controls
  include System::Windows::Media
  include System::Windows::Shapes

  def initialize(game, width = 100, height = 100)
    @window = System::Windows::Window.new
    @window.background = Brushes.Black
    @window.width = width
    @window.height = height
    @window.resize_mode = System::Windows::ResizeMode.NoResize
    @window.title = "WPF Game of Life!"
    
    game.simulation = self
    
    update(game)
    app = System::Windows::Application.new
    app.run(@window)
  end

  def update(game)
    @grid = Grid.new
    @grid.mouse_down { game.evolve }
    
    game.size.times do
      @grid.column_definitions.add ColumnDefinition.new
      @grid.row_definitions.add RowDefinition.new
    end
    
    game.state.each_index do |row_index|
      row = game.state[row_index]
      row.each_index do |col_index|
        col = row[col_index]
        
        rect = Rectangle.new
        rect.fill = col.alive? ? Brushes.YellowGreen : Brushes.DarkGreen
        rect.stroke = Brushes.Black
        
        Grid.set_row(rect, row_index)
        Grid.set_column(rect, col_index)
        @grid.children.add(rect)
      end
    end
    
    @window.content = @grid
  end

end

As long as your implementation of the Game of Life follows the rules of the challenge and implements the observer pattern with an update() callback in order to use this simulation (see my solution for an example), this should enable you to visualise your Game of Life challenge solution on Windows.

I do not, by the way, plan on officially submitting this entry to this competition purely because I do not consider myself a Ruby-newbie — although I fully expect lots of other entries from people who have been doing this half as long as I have to produce far superior solutions.

Tagged as: ironruby, ruby, wpf, game of life, challenge

IronRuby at InfoQ

Posted at 06:52 on Thursday, 20 May 2010

RUBY Some of my IronRuby starter material was published at InfoQ!

InfoQ Not too long ago, I was approached by one of my colleagues to write an article or two detailing the basics of IronRuby (from the perspective of a classic Rubyist wanted to get started with it) for publication on the very large technology community website, InfoQ. While not part of my regular job, IronRuby — the .NET-based implementation of Ruby — is one of the things that has come out of Microsoft for which I am very excited so I instantly jumped at the opportunity and spent more than a few lunch hours adapting some pieces I had written before.

The first article has been published here and another, more detailed follow-up should be appearing next week.

I hope you like it, and find it helpful. Get in touch if you have any questions:

Tagged as: microsoft, ironruby, ruby, .net, publication, tutorial

Interoperate with Microsoft Office via COM using IronRuby

Posted at 14:11 on Friday, 30 April 2010

RUBY Let’s have some fun with COM and Microsoft Office interop using IronRuby!

A new blog by the name of IronRuby Rocks! has just sprung up and yours truly will be, from time to time, contributing some content.

Today, my first piece went up on the topic of using Ruby to interoperate with Microsoft Office and COM through the close .NET ties IronRuby gives you.

It’s here:

Tagged as: microsoft, programming, ironruby, ruby, .net, office, interop, com

IronRuby 1.0 is out!

Posted at 18:30 on Saturday, 17 April 2010

RUBY If you’re a Ruby fan and you’re targeting Windows, you might be interested in this little thing called IronRuby.

IronRuby Logo
IronRuby 1.0 is the first stable version of IronRuby, targeting Ruby 1.8.6 compatibility…

IronRuby now comes in two flavors – one that runs on top of .NET 4.0, and one that runs on any earlier framework starting with .NET 2.0 SP1. The .NET 4.0 flavor features faster startup time, compatibility with C#’s dynamic keyword, and access to the new features in .NET 4.0. So, the .NET 4.0 flavor is the preferred download now, as the Microsoft .NET Framework 4.0 is publically available as of today. For Mono compatibility, use the zip file release for 2.0 SP1.

If you’re in need of some starter material, I would like to direct your attention to two articles of mine, and also to implore you to keep an eye on InfoQ for some great beginners’ articles hopefully coming soon:

Tagged as: release, microsoft, ironruby, ruby, .net, 1.0

Photo of Edd Morgan

Edd Morgan is a software developer, amateur photographer, armchair critic, atheist and lover of all things technology.

Twitter @eddm

My Game Center username is 'eddm'; so all of you can add me and we can play one of the 0 games available for it.

about 8 hours ago

That's my Jam

Bonobo
 - Black Sands

Flickr Faves

Flickr Photo Flickr Photo
Flickr Photo Flickr Photo