Ruby is really popular to make websites because of Rails, but I want to write a native looking application using Ruby. I’ve discovered wxruby which is a really cool cross platform GUI library for use with Ruby. It is built on wxWidgets which is a C++ library for making cross platform GUIs that appear native because they are built on the underlying native libraries of each platform (linux, OSX, and Windows). Right now, I’m interested in Mac, but I plan on testing this on Windows and Linux too.
You have to download the source and compile it yourself for Snow Leopard. This means, you need to install the developer toolset that comes with your Snow Leopard DVD. Just install everything, hard drives are cheap. I don’t know if you have to actually run “./configure”, “make” or not. My guess is you don’t, but I did. Snow Leopard has some special challenges and you have to do the following:
$ set arch_flags="-arch x86_64" $ ./configure CFLAGS="$arch_flags" CXXFLAGS="$arch_flags" CPPFLAGS="$arch_flags" LDFLAGS="$arch_flags" OBJCFLAGS="$arch_flags" OBJCXXFLAGS="$arch_flags" $ make
After you download, Open the DMG image and copy the contents to wherever you want. I copied mine to /Developer/Applications. When you run this for the first time, it asks you to select the location of wxWidgets.
The latest stable version is 2.0.1. If you try to install wxruby through gems, it will install 1.9.3. I don’t know what’s different between the two, but 2.0.1 is a year old so I want to use it.
$ gem install wx_sugar
You should already have a version of Ruby installed. I list it here because the latest version as I write this is 1.9.2. Unfortunately, one of the tools we will use to generate ruby classes does not support 1.9.2. If you really want to use 1.9.2, you can follow the directions here. I haven’t tried it so I don’t know if it will work or not. I just switched to mine to the system version “rvm –default system”
Making a simple app
There is a great tutorial on the wxruby website. It shows you how all these tools work together to create an application. I created my frame using DialogBlocks. I created my ruby file using xrcise. I completed the tutorial. Now if you forget that wxruby is 32bit only and you just try to run the app, you may see an error like this:
/Library/Ruby/Gems/1.8/gems/wxruby-2.0.1-universal-darwin-9/lib/wxruby2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/wxruby-2.0.1-universal-darwin-9/lib/wxruby2.bundle, 9): no suitable image found. Did find: (LoadError) /Library/Ruby/Gems/1.8/gems/wxruby-2.0.1-universal-darwin-9/lib/wxruby2.bundle: no matching architecture in universal wrapper - /Library/Ruby/Gems/1.8/gems/wxruby-2.0.1-universal-darwin-9/lib/wxruby2.bundle from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require' from /Library/Ruby/Gems/1.8/gems/wxruby-2.0.1-universal-darwin-9/lib/wx.rb:12 from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `require' from main.rb:3
To run the app, you need to run ruby in 32 bit mode like this:
env arch -i386 ruby -rubygems main.rb
Here is the screen shot of my app running on a mac.