Category: Ruby

[BUG] invalid exec_format “ir”, no %s

Posted by – May 11, 2010

Today I’ve switched from the default, C based implementation of Ruby to new version of IronRuby (1.0). After installation I wanted to update gems, install rake tool and of course at this point problems started. Command for updating gems displayed below:

igem update --system

produced output like this:

Updating RubyGems
Updating rubygems-update
Successfully installed rubygems-update-1.3.6
Updating RubyGems to 1.3.6
Installing RubyGems 1.3.6
ERROR:  While executing gem ... (Gem::Exception)
       [BUG] invalid exec_format "ir", no %s

After some research turned out that RubyGems assumes that Ruby executable contains “ruby” word in the name. In case of IronRuby the name is “ir.exe” so in my opinion this strange behavior can be treated as a bug in RubyGems. I hope it will be fixed in next release but before that will happen here are the steps to workaround the problem and update gems:

1. Create path: [IronRuby folder]\Lib\ruby\1.8\rubygems\defaults
2. Create file ironruby.rb and add it to the path from step 1
3. Open ironruby.rb and add code from the snippet below

module Gem
  def self.default_exec_format
    exec_format = ConfigMap[:ruby_install_name].sub('ruby', '%s') rescue '%s'
    #unless exec_format =~ /%s/ then
    #  raise Gem::Exception,
    #    "[BUG] invalid exec_format #{exec_format.inspect}, no %s"
    #end
    exec_format
  end
end

That is it. Now RubyGems will use new implementation of default_exec_format and everything should work as expected.

Hope this helps.

Ruby for Windows with debug support

Posted by – February 10, 2010

I mentioned somewhere in previous posts I am playing a little with Ruby and Ruby on Rails. Because I do not have an experience yet, there are a lot of situations where I need a debugger to figure out how something works. Unfortunately before I was able to debug anything I spend a lot of time trying to enable debugging in RubyMine. The main problem was in Ruby gems responsible for debugging. This gems are native C libraries and have to be compiled before usage. Of course you can guess that compilation did not work as expected. After long fight and few hours with Google I have managed to prepare a version of Ruby which can be used with RubyMine to debug applications.

Because Ruby for Windows does not require any installation I am able to easily provide you the version I have build mostly in order to save your time and do not describe whole procedure I had to follow during compilation.

The package contains Ruby 1.8.7 for Windows with following gems:

  • actionmailer (2.3.5)
  • actionpack (2.3.5)
  • activerecord (2.3.5)
  • activeresource (2.3.5)
  • activesupport (2.3.5)
  • albacore (0.1.1, 0.1.0)
  • columnize (0.3.1)
  • linecache (0.43)
  • net-sftp (2.0.4)
  • net-ssh (2.0.19)
  • rack (1.1.0, 1.0.1)
  • rails (2.3.5)
  • rake (0.8.7)
  • RedCloth (4.2.2)
  • ruby-debug (0.10.3)
  • ruby-debug-base (0.10.3)
  • ruby-debug-ide (0.4.9)
  • rubyzip (0.9.4, 0.9.1)
  • sqlite3-ruby (1.2.5)

Installation is very simple. Uncompress files to C:\Ruby directory and add C:\Ruby\bin path to PATH environment variable.

Download Ruby 1.8.7 for Windows with debug support

IronRuby/RHTML based templates for .NET

Posted by – December 8, 2009

.NET Logo

During last few days I was prototyping some of new features I would like to add to ByteCarrot application. One of them is new template engine which will replace current XSLT based solution. I wanted something with more friendly syntax, something easy to create, change and maintain. I took a look at many template engines available for .NET platform but none of them met my needs. I thought that it would be nice to have solution with scripting support and syntax similar to this available in default ASP.NET MVC view engine.

After digging I have found an ASP.NET MVC application prototype with view engine based on IronRuby. It was created by Phil Haack‘s team and its syntax was exactly the same as RHTML used in Ruby on Rails views. It turned out that this engine was exactly what I needed, so I have taken parts of the code and adapted it to my scenario. The result is amazing because now I have fully functional templates engine with support of dynamic language where I can handle every possible HTML generation scenario.

As an attachment to this post I have added the code created during prototyping. The code contains few classes where the most important is RhtmlEngine. It has very simple, self-expanatory interface and acts as an “entry point” for using mentioned templates engine. Below you can find two samples showing capabilities of my solution but if you want to take a look at the implementation and make your hands dirty feel free to download the code and play with it for yourself.

public class RhtmlEngine
{
    public Result Render(object model, string temlateFile, string targetFile)
        ...
    public Result Render(object model, StreamReader rhtml, StreamWriter writer)
        ...
}

Simple template

The templates engine requires three things to be able to generate HTML:

  • Model representing the data which should be presented in form of HTML page,
  • Template defining how model should be presented,
  • Target where generated HTML page should be saved.

Listing below shows simple template generating a header and a list of hobbies. Notice that IronRuby blocks are embedded exactly the same as Visual Basic .NET or C# code is embedded in ASP.NET MVC views.

SimpleTemplate.rhtml

<html>
    <body>
        <h1>Hi! My name is: <%= model.FirstName %><%= model.LastName %></h2>
        I like:
        <ul>
        <% model.Hobbies.each do |hobby| %>
			<li><%= hobby %></li>
        <% end %>
        </ul>
    </body>
</html>

Template shown above can be used to generate an HTML representation of the following model:

var model = new SampleModel
{
    FirstName = "Marcin",
    LastName = "Obel",
    Hobbies = new List<string>
    {
        "yachting",
        "fishing",
        "cycling"
    }
};

As you can notice looking at listing below, because the templates engine is very simple, you need only one line to change the model into HTML page.

var result = new RhtmlEngine().Render(model, "SimpleTemplate.rhtml", "SimpleHtml.html");

In result (as shown below) the code above will produce clean HTML filled in with the data from model.

SimpleHtml.html

<html>
    <body>
        <h1>Hi! My name is: MarcinObel</h2>
        I like:
        <ul>
            <li>yachting</li>
            <li>fishing</li>
            <li>cycling</li>
        </ul>
    </body>
</html>

More advanced example

The example above does not show the most powerful part of this solution which is in fact inherited from IronRuby itself. Because templates are based on fully functional, dynamic language there is a possibility to extend basic features like loops and if statements (available in all templates engines) with things like functions, classes or even use some functionalities available in .NET Framework. Next example extends this one mentioned in previous section. In the listing below you can notice two new functions:

  • encode - uses HttpUtility class from .NET Framework to encode HTML code
  • render_li – renders an item of HTML list

AdvancedTemplate.rhtml

<%
require 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
include System::Web

def encode(text)
    HttpUtility.HtmlEncode(text)
end

def render_li(text)
    writer.Write "<li>#{text}</li>";
end
%>

<html>
    <body>
        <h1>Hi! My name is: <%= model.FirstName %><%= model.LastName %></h2>
        I like:
        <ul>
        <% model.Hobbies.each do |hobby|
                 render_li hobby
             end %>
        </ul>
        <pre>
            <%= encode model.UglyJavaScript %>
        </pre>
    </body>
</html>

In order to present a usage of custom IronRuby function added to the template, below you can find extended model from previous example. Now the model contains additional property named UglyJavaScript containing JavaScript code which should be encoded before it can be presented in HTML.

var model = new SampleModel
{
    FirstName = "Marcin",
    LastName = "Obel",
    Hobbies = new List<string>
    {
        "yachting",
        "fishing",
        "cycling"
    },
    UglyJavaScript = @"<script type='text/javascript'>window.close();</script>"
};

var result = new RhtmlEngine().Render(model, "AdvancedTemplate.rhtml", "AdvancedHtml.html");

Like in previous example after passing the model and the template into engine we receive an HTML.  This time as you can see it contains JavaScript code from model encoded using .NET Framework functionality in order to be able to display it.

<html>
    <body>
        <h1>Hi! My name is: MarcinObel</h2>
        I like:
        <ul>
        <li>yachting</li><li>fishing</li><li>cycling</li>
        </ul>
        <pre>
        &lt;script type='text/javascript'&gt;window.close();&lt;/script&gt;
        </pre>
    </body>
</html>

Resources

Rake in .NET projects – installation and setup

Posted by – November 18, 2009

.NET Logo
Recently I have rewritten all MSBuild scripts which I used in ByteCarrot project to Rake. I made the decision about changing build solution mostly because I required something what works not only under Windows but also on other operating systems. After rewriting turned out that Rake is a great solution for tasks for which it was created and can be used not only with Ruby/RoR projects but also with other technologies like .NET and Mono. It is really awesome multi-purpose tool. Today I would like to show you how to install and configure Rake on Windows operating system in order to start using it in your projects. Ruby installation First of all, because Rake is based on Ruby language, you will need an interpreter. You can download Ruby distribution from its official website but I do not recommend that because there is only installer for version 1.8.6 which is quite old. Other packages for Windows on this website are in a form of compressed archives and do not contain some additional, required libraries. In my opinion the best option is to download one of preview version of installer for Ruby 1.9.1-p129 (rubyinstaller-1.9.1-p129-preview1.exe) available on RubyForge. ByteCarrot project to Rake. I made the decision about changing build solution mostly because I required something what works not only under Windows but also on other operating systems. After rewriting turned out that Rake is a great solution for tasks for which it was created and can be used not only with Ruby/RoR projects but also with other technologies like .NET and Mono. It is really awesome multi-purpose tool. Today I would like to show you how to install and configure Rake on Windows operating system in order to start using it in your projects.

Ruby installation

First of all, because Rake is based on Ruby language, you will need an interpreter. You can download Ruby distribution from its official website but I do not recommend that because there is only installer for version 1.8.6 which is quite old. Other packages for Windows on this website are in a form of compressed archives and do not contain some additional, required libraries. In my opinion the best option is to download one of preview version of installer for Ruby 1.9.1-p129 (rubyinstaller-1.9.1-p129-preview1.exe) available on RubyForge.

Ruby Logo

When the installer is on your hard drive, start installation. There is nothing magic in the installation process but you should remember that Rake does not work properly when Ruby in installed in C:\Program Files directory (probably because of space in the path). Because of that leave the default installation path or change it to something without spaces like for example C:\Ruby.

Last thing you should do to be able to use Ruby is adding the location of its binaries to the %Path% environment variable. In my case this variable was extended with C:\Ruby\bin path.

Now you can check if everything works by executing from command line two following commands:

ruby –v
gem -v

The output from console should be:

C:\>ruby –v
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mingw32]
C:\>gem –v
1.3.4

Rake installation

Now, when Ruby is installed and you are sure that it works you can take care of Rake. The most popular and safe way to install Ruby extensions and libraries (including Rake) is mechanism called Gems. In order to install Rake using Gems execute following command from command line:

C:\>gem install --remote rake

The output from console should be:

C:\>gem install --remote rake
Successfully installed rake-0.8.7
1 gem installed
Installing ri documentation for rake-0.8.7...
Updating class cache with 0 classes...
Installing RDoc documentation for rake-0.8.7...

When the command will finish do not close command line window yet. At the end execute one more command to be sure that Rake is installed properly:

C:\>rake –V

The output from console should be:

C:\>rake –V
rake, version 0.8.3

Congratulations! You have managed to install Rake and you are ready to write your first build script. More about Rake can be found on official site of the project. Example Rake scripts can be found in ByteCarrot source code on CodePlex.

My beginnings with Ruby

Posted by – August 23, 2009

Ruby LogoSince some time I have had a feeling that I should turn my eyes in direction other than .NET and technologies connected to it. Maybe not as a new path of career, because I have invested a lot of time to get to the point where I am now, but as something additional what can give me some new perspective and help to become a better developer. Because I cannot learn all available technologies I had to choose one and only one of them. After reading some blogs and mostly because of Jeremy D. Miller’s opinion my choice fell on Ruby and the Rails. So many developers are excited about this technology, I decided to find out what is a source of theirs excitement.

So how am I going to learn Ruby? First of all I always try to start from a book. In this case I am going to read (in fact I have already started yesterday) Polish translation of “Ruby in a Nutshell” by Yukihiro Matsumoto. I think and the author has the same opinion that this is not the book for someone who is new to programming and wants to learn his first language. The “Ruby in a Nutshell” is something like language reference but in more accessible form so for me (developer with above 8 years of commercial experience) is perfect. I do not need 20 pages of description how to use while loop or what are iterators and where can be used.

I am not able to read this type of book from end to end without touching computer so during reading I am going to play a little bit with the language itself in a more practical manner. In order to do it I had to setup a development environment and there I faced first problems. Turned out that current version of Ruby (1.9) for Windows does not have official all-in-one installer. There is installer for version 1.8 and some preview installers for 1.9 which do not work perfectly (at least on my Windows 7 machine). Because of this I had to install Ruby manually using binary distribution and again I had problems. Of course binaries were in a form of compressed file so I uncompressed them and moved to C:\Ruby directory. Secondly I have added C:\Ruby\bin\ path to the environment variables. In my opinion this should be all I have to do to start learning but turned out that I was wrong. The official binary distribution does not contain all required libraries and was crashing from time to time. After some googling I have found very useful blog post (unfortunately I have lost its address) which solved my problems pointing where I can find missing libraries and where should I put them.

When I had Ruby environment ready to work, theoretically I was able to start playing with it but … I am .NET developer and I used to use reach IDEs with syntax highlighting, suggestions, debugging, refactoring tools etc. I decided to find such tool but about it, learning progress and my thoughts about Ruby from perspective of my quite long experience with .NET I am going to write in some of future posts.