Archive for the 'IronRuby' Category

01DecAdding a console to your IronRuby application

When building an application it might be very handy to have a REPL console that knows about the libraries of your application, but you don’t necessarily want to start your application to interact with it.  In Rails they have a script/console command.  Here’s how you create one that knows about ironruby. The example I’m going to use is taken from an IronRubyMVC application.

I started out by creating a folder script.

Then I created a file called console (on a unix system I would chmod +x this file). I also like to have completion in my console so I’ve added the irb/completion library.  Then I’ll require the routes.rb file so that the libraries of my application get loaded.

#!/usr/bin/env ir
# File: script/console
irb = ENV['OS'] =~ /^Windows/iu ? 'CALL iirb.bat' : 'iirb'

libs =  " -r irb/completion"
# Perhaps use a console_lib to store any extra methods I may want available in the console
libs <<  " -r #{File.dirname(__FILE__) + '/../routes'}"
puts "Loading Poll chat"
exec "#{irb} #{libs} --simple-prompt"

The 3rd line has CALL iirb.bat as a command on a windows system. The CALL command is needed for the next step because we’re going to execute a batch file from another batch file on windows. Otherwise it wouldn’t work for me.   CALL is very similar to exec in ruby and gives control to another executable until its task is done.

For windows to be able to use script/console (script\console) instead of ir script/console you also need to create a batch file called console.bat in the script folder.

@ECHO OFF
IF NOT "%~f0" == "~f0" GOTO :WinNT
@"ir.exe" "script/console" %1 %2 %3 %4 %5 %6 %7 %8 %9
GOTO :EOF
:WinNT
@"ir.exe" "%~dpn0" %*

This is all there is to it to get rails like scripting abilities.

IronRuby has another really cool feature built into IronRuby is the ability to provide REPL’s for your application at run-time. All you need to do is use Repl.new and give it an output and input stream.

26OctCreating launcher scripts for IronRuby

It’s been a while since I blogged, I’ve been terribly busy going through some changes and prepping the book.

Anyway lately a lot of blog posts have been written on how to ironruby with cucumber to test your .NET code. While I think it’s great people are using ironruby and cucumber, the guide you can find on aslak’s github wiki isn’t the most ideal solution as it will  only work for windows and it requires MRI to be installed on your system.  So I thought I’d write up how I’ve been creating launchers that work both on windows .NET and mono systems.

Another problem the approach of setting the GEM_PATH to the MRI gem location is that if your gem requires a C-extension (which could easily be a C# extension in IronRuby) ruby will get confused about which one it’s going to need.

I’m going to use cucumber as an example but this counts for most ruby libraries. I’ve been using this for a few months already so it really doesn’t matter which version of IronRuby you’ve got installed. I’ve compiled a fresh version from github and deployed that to C:\ironruby on windows and added C:\ironruby\bin to my PATH environment variable. I installed my ironruby version on my *nix boxes in /usr/local/ironruby and added /usr/local/ironruby/bin, /usr/local/ironruby/silverlight/bin and /usr/local/ironruby/silverlight/scripts to my PATH environment variable.

1. install the gem: igem install rspec cucumber –no-rdoc –no-ri

this will install the rspec and cucumber gems with their dependencies. And the gems process will actually install the launcher scripts in C:\ironruby\lib\ironruby\gems\1.8\bin and we’re going to use those scripts to create our launcher script

2. Get the launcher scripts into the bin dir

On windows you can now go:

copy C:\ironruby\lib\ironruby\gems\1.8\bin\cucumber C:\ironruby\bin\icucumber
copy C:\ironruby\lib\ironruby\gems\1.8\bin\cucumber.bat C:\ironruby\bin\icucumber.bat

NTFS supports symlinks so you could also use the junction tool from the sysinternals toolkit to create those instead of copying the files. http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx

On *nix based systems there is one more step to go through.

cp /usr/local/ironruby/lib/IronRuby/gems/1.8/bin/cucumber /usr/local/ironruby/bin/
mv /usr/local/ironruby/bin/cucumber /usr/local/ironruby/bin/icucumber
cp /usr/local/ironruby/lib/IronRuby/gems/1.8/bin/cucumber /usr/local/ironruby/bin/icucumber
chmod +x /usr/local/ironruby/bin/icucumber

And this is the easier way to properly use installed gems from the ironruby distribution, it will also make it a lot easier to upgrade your gems in 2 different ruby installations at different times etc.

02JulA rant on a talk falling to pieces

Earlier this week I had the pleasure of doing a talk for the Belgian .NET user group.  During this talk I ran in to all kinds of problems. I’ve done presentations where I was royally underprepared and to avoid that this time I actually started prepping for this talk on time. I was done on time, was prepared had 5 backups of my presentation and samples. What did I learn from this, if you’re prepared other things will go wrong.
I’ll share the story of stuff that can go wrong.

Before starting the rant I have some links to share.

I have a device that I call internet on a stick, which is a vodafone usb 3G modem that I plug in to my system and it gets me on the internet (mistake 1). Since I assumed that that thing would continue to work I made most of my demos internet enabled (mistake 2). For example I have a demo where I go download pics from flickr and then show them with some animations with silverlight.
Before my presentation I changed the fonts, opened all the files I was going to talk about ran all the demos again to make sure they would work and everything went fine.
I unplug the USB device and go into the room to hook up the projector etc. The presentation starts and for the first hour everything went really well (from my perspective at least don’t know about the people attending).
We have a break and I plug the USB device in, at this point the vodafone program hangs (first time ever I swear). What’s more I can’t make it go away at all so I reboot my pc (this is still during the break).

Now I’m getting a little desperate because it still doesn’t work. With rebooting I also lost all my carefully opened files earlier (I’m showing code in about 3 different environments and 2 different OS’es).
So during the presentation I apologise and try to reboot once more while taking questions from the audience and hoping somebody will try to start a discussion with me. After rebooting I got a message saying my date was set to 2001 which I thought was peculiar but clicked it away.  I boot vmware fusion with windows 7 (this took fairly  long and is a little bit funky as the screen resolution changes a lot during this process).
Ok so far so good, by now I’ve already skipped the silverlight demo promising that it will be available as a download on my blog and will be moving on to the ironrubymvc sample. To prove I do actually use visual studio at times I wanted to open my demo project in visual studio. I open visual studio only for it to tell me that my trial has expired and I can either upgrade or close the application. Oooooookay this is completely weird because i get my software through my msdn subscription and I had been using it earlier that day.

Moral of the story: Either go vastly underprepared and wing it. Or don’t rely on the internet and always take at least 2 laptops that have identical configurations but I’m pretty sure those would or explode in my face or something will fall from the ceiling, building collapses or other mishaps.

Instead of having one demo go bad on me now I’m probably facing a reinstall of my mac because it lost a bunch of settings, for which I’m holding the vodafone responsible. At this moment I’m fairly certain that I should go less prepared and just wing it just out of fear for bigger disasters, people may die.

09JunVery simple IronRuby MVC sample application

Over the weekend I wrote a very simple task list application with IronRuby MVC and LightSpeed as an OR/M.  And I’ve just put a demo live. This demo is running on a windows 2008 vps with IIS 7 as webserver. The source of this sample is also available.

Continue reading ‘Very simple IronRuby MVC sample application’

21MayGetting started with Caricature and IronRuby

IronRuby 0.5 was released yesterday. You can download it on codeplex. This post will explain how you setup your ironruby environment to use it for testing existing CLR based assemblies.  We’ll touch installing gems using rake and most importantly writing a test for a CLR based class where we’ll mock out the dependencies.

Continue reading ‘Getting started with Caricature and IronRuby’

19MayCaricature ready for beta

The last couple of days I’ve been getting Caricature to a more releasable state. The code got a thorough cleanup and refactor. Caricature now knows how to be a full mocking framework for Ruby classes and CLR classes that only interact with ruby objects. When I apply the 80/20 rule to caricature it’s definitely beta worthy.

Continue reading ‘Caricature ready for beta’

15MayAn update on caricature

Yesterday I explained about caricature but I didn’t have it fully tested yet. Today I’ve written a bunch of integration specs for all the platform combinations Caricature currently supports so you should be able to use Caricature also for plain ruby object mocking in addition to CLR interop mocking.

After having a very brief twitter discussion with Scott I decided it would be a good idea to change the name of the methods when\_told\_to and was\_told\_to? . In ruby objects receive messages so the wording now becomes when\_receiving and did\_receive? . I have a problem with the should naming because there is nothing conditional about it. Either you want the method to return something or you want it to return a default value for CLR value types or nil.  And if you’re asserting if a method it called that is deterministic; it is either called or not there is no gray area there.


ninja = ClrModels::Ninja.new 

@weapon.when_receiving(:attack).with(ninja).return(5) 

@ninja.attack(ninja, @weapon).should.equal 5 

@weapon.did_receive?(:attack).with(:any).should.be.successful 

Before I call it 1.0 I want to at least give ruby objects also the ability to isolate static and sealed methods as well as mocking ruby class methods.  So that for CLR to Ruby interaction you a get the full range of possibilities.

I have also set up a site on github that contains the API documentation for Caricature. When IronRuby 0.5 releases I’ll publish a blog post that will talk you through testing your CLR assembly with IronRuby, bacon and Caricature.

For now you can take a look here for example on how to use Caricature for isolating features.

14MayIronRuby just got a mocking framework – kind of

As I mentioned in a previous post.  I started working on a small mocking framework. It has now progressed far enough to handle the most common mocking tasks.

Below I pasted the output of the integration tests for CLR interop.

when isolating CLR interfaces
- should work without expectations
- should work with an expectation with any arguments
- should work with an expectation getting different method call result
- should work for an assertion on a specific argument

when isolating CLR classes
- should work without expectations
- should work with an expectation for any arguments
- should work with an assertion for specific arguments
- should fail for an assertion with wrong arguments

when isolating CLR instances
- should work without expectations
- should work with an expectation for any arguments
- should fail for an assertion for specific arguments
- should allow to delegate the method call to the real instance (partial mock)

you will need bacon installed to run the specs. you should issue the command igem install bacon for that.

you can then install the caricature gem in ironruby by issueing

igem install caricature

To use it there are some examples in the file spec/integration_spec.rb


require 'rubygems'
require 'bacon'
require 'caricature'

ninja.when_told_to(:survive_attack_with).return(5) 

weapon.attack(ninja).should.equal 5 

ninja.was_told_to?(:survive_attack_with).with(:any).should.be.successful

There is a gotcha though, when you use it in a CLR class you’re bound to CLR rules and it only overrides the methods that are marked as virtual. We also can’t isolate static or sealed types at the moment.

I took the approach of doing away with the terminology of mocking and subbing and instead chose the much clearer Isolation. By default any method returns null or the default value of a value type. You can tell an isolation to return a specific value or raise an error etc.  Later on you can then assert if the method was actually called. 

This fits in better with the way you probably structure your tests.

I hope you like it.

You can find the source in my github account.

http://github.com/casualjim/caricature

03MayMocking for IronRuby

As you may or may not know I’m in the process of building IronRubyMvc. At one point I did write a bunch of tests in for the code I had at that point. However that test code was written in C# with xunit and moq.  I wasn’t too happy about that so I deferred writing tests to a later date when I could use a Ruby library to write them.

That time has come, I can use bacon and it’s acceptable performance wise now, not stellar but workable. So when I started to port some of my previously written tests to bacon I ran into a road block. Mocking isn’t as straightforward as my optimistic self was expecting. This lead me to think about what exactly does a mocking framework do?

Continue reading ‘Mocking for IronRuby’

02MayUsing IronRuby with RubyMine

RubyMine was released earlier this week. I’ve used rubymine in the past and was very impressed with the IDE, so much that I’m silently hoping the guys from Jetbrains would do a C# IDE too. I’d probably buy that one as well, but for now I’ll settle for Resharper with Visual studio.

By default Rubymine won’t accept IronRuby as a ruby SDK, but I asked for help in the twitterverse and got it (http://twitter.com/iRomeo/statuses/1677867948).

settings_rubymine

Just symlinking didn’t really work because the ruby.exe listens to –version for getting its version number.  IronRuby on the other hand listens to –v to get its version number.  The solution to that is to create a batch file that will translate the –version  modifier to –v.

So instead of symlinking the ir.exe to a name that starts with ruby I created a batch file called ruby_iron.cmd with the following content.

@echo off
set IR_CMD="%~dp0ir.exe"
if "%1"=="--version" ( GOTO RUBY_VERSION )
GOTO RUBY
:RUBY_VERSION
%IR_CMD% -v
goto QUIT
:RUBY
%IR_CMD%
:QUIT

With that ruby_iron.cmd file in place you can just add IronRuby as an SDK in RubyMine.

Enjoy :)


Recent Flickrs

    Blogroll

    Recent Listening

    Scrobbler