Ruby Tuesday #22 : First steps with IronRuby and Silverlight
With the recent release of Silverlight 2 and the latest version of the Dynamic Languages SDK, now seemed like a good time to write a simple Silverlight application in Ruby. Once you’ve got all the bits downloaded, installed and extracted, find yourself a fresh new folder and open a command prompt. I should note that I put the scripts folder of the Dynamic Languages SDK on my path variable before opening the command prompt. At the prompt type:
sl ruby <application_name>
replacing <application_name> with the name of your application. To see it running type:
server /b
Navigate to index.html and there’s the app running.
Right – time to fiddle. Open the app.xaml file and add a button. Here’s my xaml file:
<UserControl x:Class="System.Windows.Controls.UserControl" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid x:Name="layout_root" Background="White"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="100"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="100"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock x:Name="message" FontSize="30" Grid.Column="0" Grid.Row="0" /> <Button Margin="4,4,4,4" FontFamily="Verdana" FontSize="18" Grid.Column="1" Grid.Row="0" Content="Push Me!" x:Name="rubyButton" /> </Grid> </UserControl>
Now, we just need to do something when the button is clicked. Open the app.rb file and update the initialise method so it looks like this:
def initialize
message.text = "Welcome to Ruby and Silverlight!"
rubyButton.click do |sender, e|
message.text = "Hello World!"
end
end
Reload your app, click the button and whoop with delight. Worth looking at index.html in an editor – the documentation will prove helpful.
ASP .NET in IronPython
If you’ve ever wanted to use a dynamic language and target the ASP .NET platform, you’re in luck. First step is to visit this download page and download the samples and documentation – I’m assuming you already have IronPython installed. The documentation contains a walkthrough and some how-tos. There’s also a useful post from Jimmy Schementi here that guides you through an introductory example step by step. I always like samples that run as expected and are simple enough to understand quickly.
Made Up Technical Terms #15
Crowd Computing – The use of large numbers of people to solve a problem.
Mobile multithreading
While it is still (relatively) fresh, I thought I’d share my experiences writing some multithreaded code for the .NET Compact Framework. You can read the article here.
Interview with Ra Wilson of National Rail Enquiries
Ra Wilson, who works for National Rail Enquiries, was a member of the team that developed the proof of concept I posted about recently. Ra, in addition to being a domain expert, rolled his sleeves up and got his hands dirty with some Silverlight development. I thought it’d be interesting to hear his thoughts about the PoC and developing in Silverlight and Ra generously agreed that I could post an interview with him.
Ra, could you tell us a little about National Rail Enquiries and what your role is there?
National Rail Enquiries is the official source of rail information for Great Britain. We have a phone line and a website. The website takes care of over 80% of our customer contacts. Our main pieces are journey planning, live departure boards (real time information), and train station information. I’m the Web Development Manager here. I spend about 50% of my time doing due diligence on technical solutions proposed by our suppliers and the 50% writing code and managing contract developers.
What’s your web development experience?
I have about 8 years experience in web. I started off designing user interfaces for mobile and desktop browsers, manipulating images and writing WML, XML, HTML and CSS. Pretty early on I learnt JavaScript so I could create richer user experiences and I also have some experience of classic ASP (VBScript). I used to think of myself as a web designer but now I mostly leave that to guys with better hair cuts than me.
We used Silverlight during the PoC to create a rich user experience. Some people use Flash – have you any experience of using Flash?
I had done a little bit of Flash work about 5 years ago. I found actionscript frustrating; the almost JavaScript syntax confused me, and having all the code scattered throughout the move makes it hard to pick up someone else’s work. I believe the tools are better now though.
You wrote some of the Silverlight code in the PoC. How did that work out?
The 3 week PoC with DPE [Developer and Platform Evangelism at Microsoft] was the first time I’d worked in a C# and Silverlight environment. I was struck by how easy it was for designer and developer to work together, even on the same file at the same time! Obviously both designer and developer are still required to have their brains switched on and being able to converse is essential, but I didn’t hear any of the usual “you’ve made my design look like 1990 clipart” or “but you can do it in Photoshop…” banter that can often cause team tension and unrealistic expectations from clients.
How did you find learning Silverlight?
I found the XAML pretty easy to pick up. It feels like HTML on steroids without the mood swings. If HTML was this easy to create the same experience cross browser I could get a LOT more done.
What about the programming aspect of it?
The C# was quite a steep learning curve but the intellisense helped a great deal. It’s very exciting to have such a powerful language exposed though Silverlight making it cross platform cross browser. I’m very tempted to lock myself away in a dark room for a couple of weeks and really get a handle on C#.
Now that the PoC is complete, what are your reflections?
The PoC was certainly an inspiring time for me. Silverlight looks like a great product to create rich web applications in, maybe even whole sites! Although I think search engines will need to up their game with the way they trawl and weigh this type of content before I can give up writing JavaScript, HTML and CSS hacks.
PDC
PDC is coming up fast. I’m heading out for the conference in a couple of weeks and was just reviewing the sessions I’d like to attend – spoilt for choice. Worth keeping an eye on the blog and the twitter feed for the latest info. Those of you attending will be getting a little something.
Ruby Tuesday #21 : Closures
It seems impossible to avoid closures when reading about Ruby. What is slightly unclear is what exactly one of these closure things is and what they should be used for. According to Wikipedia, a closure is:
"a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables"
Not much clearer? Here’s Martin Fowler’s contribution:
"Essentially a closure is a block of code that can be passed as an argument to a function call."
He goes on to explain that a key feature of closures is that:
"they are a block of code plus the bindings to the environment they came from"
Which leads us back to the Wikipedia definition. He also points out that in a language that doesn’t support closures, he misses them. I get the feeling that this is a language construct which is best understood by usage rather than explanation. And there’s some stylistic preferences. Consider this example from a useful primer on closures:
@person = Person.find(id)
respond_to do |wants|
wants.html { render :action => @show }
wants.xml { render
ml => @person.to_xml }
end
The author describes this code as "beautiful to look at, and with a quick glance, you can tell exactly what it does" – I’m not sure I agree on either point. I’ve seen a fair amount of Ruby that has impressed me with its expressiveness – this excerpt doesn’t make that list. In my search for more insight into closures, I found this is a good starting point – an executable piece of exploration (found via RubyInside.)
LINQed Lists Article
While working on the National Rail Enquiries Proof of Concept, I wrote some code to filter lists and select items from lists that used LINQ and ran on the .NET Compact Framework. There’s more detail in this article.
The Age of the Train
Over the last few weeks, I’ve been part of a team from Microsoft, Conchango and National Rail Enquiries building a proof of concept. On Wednesday 1 October, at the TechNet event in London at which Steve Ballmer spoke, we showed a live demo of some of what we had built in the three week proof of concept. The concept we proved is the use of National Rail Enquiries services in multiple channels. National Rail Enquiries already have a very popular Vista gadget available that allows you to see live train departure information on your desktop. Prior to starting the proof of concept, we held a two day architectural workshop to establish what services and applications rail users need to make their experience of using trains even better. The priority sample applications were an Outlook Add-In, a Windows Mobile application, a new way of presenting live train information on the web and an internal dashboard application. Those of you who attended the TechNet event on Wednesday will have seen the first three of these applications in action.
The Outlook Add-In enables users to book travel time. Most of us have appointments in our Outlook calendar. Some of these appointments require travel. The add-in lets you select the appointment and plan your train journey to and from the appointment. When you select the trains you want to take, it books time in your diary automatically – in the detail of this travel time there is information about the selected journey and an earlier and later journey should everything not run to plan. It’s simply formatted so that it reads well on your mobile device. It also links directly to the live train information on the web.
Speaking of the web, we set out to build something that would enable you to get the information you want quickly and as intuitively as possible. So, we settled on using Silverlight using DeepZoom to navigate around Virtual Earth imagery, on top of which we overlay track and train information. You can zoom in to your chosen station and get station information, see the trains in the area (blue for on time, red for late) and get information on them, too. And, even better than that, the train positions are updated so they move. Here’s a peek at what it looks like:
The map can be embedded in the National Rail Enquiries site and there’s a button that allows you to expand it to a full screen view (which is the view I’ve used above.) Given the popularity of the National Rail Enquiries site, scalability was a primary consideration. When you think that there are in the region of half a million visits per day to the site and thousands of trains, you can see it would be easy to design an application that would not scale. We used a state service which holds a model of the train network on the server – thus minimising the network traffic that would result from this model being held on the client given that every client would have to get updates from the server. This way a client only gets the information it needs. The other thing to note about the service is that it has SQL Server 2008 at its heart – we made usage of the geospatial capabilities to store information about tracks and stations.
The last application we demoed was a mobile phone application that enables you to check what time the next train leaves, if it’s on time, when it’s due to arrive and what platform it’s leaving from. Given that this is a mobile application, it’s written using the .NET Compact Framework 3.5. It takes advantage of local storage (for instance the list of stations is held on the device) and features of the compact framework such as multithreading while using a web service to get live information.
All in all, I think this is a great demonstration of how your architecture can cover multiple delivery channels taking advantage of different devices. I’ve already been using the mobile application (someone needs to test it) – and my journeys have involved less waiting in stations as a result.