Tools

Dynamic Language Plus .NET

By Shay Friedman, April 30, 2010

IronRuby brings the two together

Testing

When it comes to testing, IronRuby brings with it innovative Ruby testing frameworks that can change the way .NET developers test code by making the process simpler, faster, and more interesting than before.

For example, the RSpec testing framework provides a domain-specific language for unit tests. The following RSpec code tests a .NET method:

describe System::Collections::Stack, "Count" do
it "returns 0 for empty stack" do
stack = System::Collections::Stack.new
stack.Count.should == 0
end
end

Cucumber is a testing framework that follows Behavior Driven Development (BDD) principles. The idea behind the framework is simple but ingenious -- let project managers write the specifications for a feature, but require them to follow formatting guidelines. Developers, in turn, write code that interprets the specification document. Cucumber then combines these two and the specifications are executed as tests. For example, the following sample is a specification document that tests the .NET String class. Note the Given-When-Then format: Given describes the prerequisites, When describes the action, and Then describes the expected result:

Feature: Verify that System.String is working
Scenario: Test Trim method
Given I have a string that contains " IronRuby "
When I execute the Trim method
Then I should receive "IronRuby" as the result

The code that interprets the spec uses regular expressions and has three matching methods the developer should implement -- Given, When and Then:

Given /I have a string that contains "(.*)"/ do |str|
@str = System::String.new(str)
end
When /I execute the Trim method/ do
@result = @str.Trim
end
Then /I should receive "(.*)" as the result/ do |actual|
@result.to_s.should == actual.to_s
end

When the spec document and the code are ready, Cucumber is run via the command line to execute the tests. You get the following output:

Feature: Verify that System.String is working
Scenario: Test Trim method
Given I have a string that contains " IronRuby "
When I execute the Trim method
Then I should receive "IronRuby" as the result
1 scenario (1 passed)
3 steps (3 passed)
0m0.572s

Silverlight

Silverlight provides a rich GUI forWeb applications, much like Adobe Flash. Until Version 2, developing Silverlight apps could be done solely by .NET static languages like C#. Since Silverlight 2 includes the DLR, it allows writing Silverlight apps in dynamic languages like IronRuby.

The Silverlight and DLR bundle brings great things to both Ruby and .NET programmers. Ruby developers get a rich Internet application framework they can use in their language, and .NET developers can use the dynamic capabilities of Ruby, such as the ability to create a REPL
(Read-Evaluate-Print-Loop) window.

Microsoft's Gestalt project took Silverlight in a different direction, letting developers interact with the browser in Silverlight-supported languages, rather than in JavaScript. Gestalt allows adding <script> tags that contain IronRuby code (IronPython and XAML are supported as well), replacing JavaScript with Ruby for client-side scripting.

To use it, include a JavaScript file named dlr.js, then write Ruby code inside <script language="ruby"> tags. For example, the following HTML page contains a textbox and a button: Once the user writes his or her name and clicks "Congrat!", a Ruby script shows an alert
with a welcome message and adds the message to a <div> element on the page:

Ruby On Rails

Ruby on Rails is an MVC-driven framework widely used to develop Web applications for big sites like Twitter, Yellow Pages, Hulu, and others. IronRuby lets .NET developers use their C#/VB.NET codebase and still take advantage of the Ruby on Rails framework. Furthermore, since the.NET framework is integrated with Microsoft's Internet Information Services, IronRuby becomes the top choice for running Ruby on Rails apps on IIS.

In sum, making Ruby part of the .NET Framework via IronRuby, offers enormous benefits to .NET developers, just as bridging .NET and IronRuby benefits Ruby developers. As soon as both developer camps understand the potential in this, we'll see IronRuby projects erupting everywhere. It's as simple as that.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!