Demystifying Iron Ruby in .NET

Abstract

The developer community has radically diverted their attention towards Ruby programming because it is strongly typed, open-source, and easier to manipulate characteristics, even becoming the popular programming language in the security professional community in terms of exploit development. Programmers now prefer to write a moderate level of code instead of relying on the exhausting process of coding thousands of lines of code to achieve a specific functionality feat.

Unfortunately, the Ruby language was restricted to use on specific platforms before the invention of IronRuby. Hardcore .NET programmers could not operate it in the .NET CLR context. But now after designing IronRuby, .NET developers can get a taste of the dynamic programming Ruby in the .NET CLR. This article is therefore especially designed to explain the running mechanism of Ruby programming code under the .NET context.

The aspirant would be able to understand as such, how to install, configure, and run arbitrary Ruby code using the Visual Studio IDE and what the importance of the .NET dynamic language runtime is, to manipulate Ruby programs. By the end of this article, the developer will be able to write a moderate level of Ruby language coding. For instance, using the Visual Studio 2010 IDE, what a data type is, the control structures, and what the objects are.

Prerequisite

The Visual Studio IDE typically does not include IronRuby in its project templates such as other CLR-supported programming languages. However, the Ruby language is already available with several IDEs. For instance, NetBeans, Ruby in Steel, and RubyMine. In this article, we shall explore essential IronRuby programming hands-on using .NET Framework integration in the Windows operating system. Hence, it is necessary for the following software pertaining to Ruby Programming to be installed in the system.

  • IronRuby MSI
  • Visual Studio 2010 or later
  • .NET Framework 4.0 or Later
  • Sharp-developer Studio, Notepad++ (Optional)
  • Windows Operating System (either Win7 or Win8)

Installation and Configuration

Although we can write and execute standalone IronRuby program code in the Ruby Console itself this article is specially designed to emphasize the open-source capability of the Ruby language by depicting the essentials of IronRuby hands-on through Visual Studio 2010 or later IDE. Hence, first, install IronRuby to enable its presence under the Visual Studio project template. IronRuby could be installed either by MSI itself or through the Visual Studio NuGet package console.

IronRuby MSI

The IronRuby MSI typically can be downloaded from ironruby.codeplex.com/releases/view/49097/. Once the IronRuby MSI executable has been downloaded and the installation phase has been done in the Windows operating system (the installation process is relatively easy compared to other platforms, for instance, Linux and Mac) IronRuby will be added to the Visual Studio 2010 project template as in the following.

 IronRuby MSI

Nuget Package

In fact, installing IronRuby from NuGet is a rather easy process and could be initiated just by issuing the Install –Package Iron Ruby command in the Visual Studio console as in the following.

Nuget Package

Whether IronRuby has been installed through MSI or the Nuget console, its corresponding directory will be in the system Program File directory where we can ensure successful installation by hitting the ir.exe command as in the following.

 Nuget console

The ir.exe command is employed for interpreting the Ruby code at the interactive shell. One of its plug-ins is also added to the Visual Studio IDE automatically after the installation. This interactive shell can therefore be accessed from the Visual Studio IDE by going to the Tools | IronRuby Tools | IronRuby Interactive menu, where we can interpret or run basic Ruby code as in the following.

Ruby code

The IronRuby console can be directly run, once its corresponding installation directory path is properly configured in the environment variable. The following Ruby Interactive shell gives us the same coding experience as earlier.

 Ruby Interactive shell

IronRuby Mechanics

IronRuby truly is an Open Source implementation from Microsoft for executing or interoperating Ruby programs with the .NET framework. IronRuby could be defined as the best paradigm of a Read-Eval-Print Loop (REPL). IronRuby leverages the built-in capabilities of the Ruby language and depends on the Dynamic Language Runtime (DLR) to conglomerate the power of both the Ruby programming language and the .NET Framework built-in FCL. IronRuby incorporates the notion of meta-programming constructs that enable prolonging any class object at runtime, where methods and behavior can be changed dynamically. The major benefit of IronRuby programming is that it lets Ruby code access a considerable variety of .NET Framework class libraries and services because the CLR endorses Ruby programming. Moreover, IronRuby can be run from any browser through Silverlight add-on support. We have obtained much understanding of the importance and benefits of Ruby language programming but now the question arises as such, how can the Ruby language actually be run in the .NET Framework context, how to interact with the .NET Framework built-in class libraries using the Ruby language? For this, the following figure illustrates a brief preview of the compliance of the Ruby language with the .NET framework.

 IronRuby programming

As in the aforesaid figure, the DLR is the backbone of the Ruby dynamic language that is located on the top of the .NET CLR to lay the foundation of any typed language such as IronRuby or IronPython. Here, the Ruby code is initially parsed by the IronRuby engine which is later converted to DLR grammar. Such standard grammatical code needs to be provided by the DLR by all the language implementations for executing the inter-operable code. The DLR then converts that corresponding grammar code into MSIL intermediate language that is in fact, comprehensible by all. NET-supported language to establish cross-language communication. This is the actual mechanism to inter-operate the Ruby language with the .NET Framework because ultimately all parsed code must compile into IL code.

The Hello World Program

This section will illustrate how to create your first Hello World program using the Ruby language in the .NET Framework context, by loading the corresponding IronRuby project template from the Visual Studio 2010 IDE. Hence, first, be sure that IronRuby is properly installed then select the IronRuby language from the Visual Studio New Project template section. Here, we shall notice several project types have already been loaded to choose from as in the following.

Ruby language

Here choose Console Application and assign a meaningful project name because this first "hello world" program will elaborate through a typical REPL interactive interpreter at the command shell. Thereafter, notice that in the Solution Explorer, the program. rb has been automatically created which could be either renamed or remain the same to operate in the current solution. Hereby, it is not a big deal to implement the first "Hello World" program in Ruby, it could just be done using simply put methods of the Ruby programming construct.

Listing 1. string “Hello World” displaying.

puts "Hello World"

We can also integrate some typical OOP constructs to accomplish this task. Hence, just place the following code in the program.rb file that contains a class named Test. The "Hello World" string will be displayed via the method Method_Test where we are passing the second argument via command line construct.

Listing 2. OOP construct demonstration in IronRuby.

# Class declaration
class Test
  
  # Method definition
  def Method_Test
    # Displaying output using 'puts'
    puts "Hello #{@arg}"
  end
end
# Class end

# Class initialization 'Object creation'
obj1 = Test.new("Ajay")

# Method calling
obj1.Method_Test

Once done with such coding, save the entire solution and build it either through the F6 button or from the menu and observe the output. Simple! We can see the output from Visual Studio 2010's built-in Ruby interactive shell in the following.

Ruby interactive shell

Note. It is not necessary to rely only on Visual Studio to compile this Ruby program. Instead, we can use other editors like Notepad++ to do it.

Or by using the ir.exe utility at the Windows command prompt. Here, the ir.exe utility is doing both operations, the interpretation of the code as well as yielding the output, by executing the following command.

Windows command prompt

Note. The Ruby source code file can be placed anywhere in the system memory when compiling using ir.exe.

However, we have encountered a couple of OOP's constructs (for instance class, constructor, methods, and so on as in a typical C# or VB.NET code) in that sample, but we needn't bother to delve into such concepts in this scenario because the simple "Hello World" printing can be done using the puts method of Ruby programming.

Elementary Programming Constructs

IronRuby is truly an object-oriented programming language. Ruby programming is also capable of operating an entire essential OOP implementation in a similar fashion that C# and VB.Net. The Ruby language is generally run in an interpreted atmosphere and does most of its operation at runtime, hence it is modifiable at runtime. We shall briefly discuss the rudimentary programming construct of the Ruby language, like how to declare a variable, how data types are dynamically allotted, and how to play with essential arrays and method declaration constructs in the forthcoming section.

Variables and Data Types

The type information of variables while the declaration is typically undefined in IronRuby programming because of its dynamically typed language nature. All the data types are assigned implicitly. Here, we are declaring a variable as an "item" whose type identifier is missing as in the following.

Listing 3. numeric value assignment to a variable.

item = 1

So, if we have assigned numeric data to an undefined variable then we can easily conclude that the variable "item" is an integer type. Moreover, if we assign any string type data to any variable then it‘ll automatically be referred to as a string data type as in the following.

Listing 4. String value assignment to a variable

name = "ajay"

Note. Coding in IronRuby is a strictly case-sensitive language like C# programming and all the other languages derived from the C language.

Array

Arrays in IronRuby are mutable unlike C# and can be defined using square brackets to assign a range of values to a variable such as in the following.

Listing 5. Array's declaration

days = ['Sunday', 'Monday', 'Tuesday']
puts days.join(', ') + '!'

As in C#, the following code sample shows an array of integer types.

Listing 6. numeric array declaration

Age = [23, 45, 12, 78]

Note. Comments can be declared by the # symbol in IronRuby code that literally instructs the compiler not to execute the specific line of code.

Function

Functions or methods are pieces of code that can be called or invoked anywhere in the source code or API. Methods can be defined by the def keyword in the Ruby language and their bodies typically culminate in an end keyword.

Listing 7. Function body declaration

def Method_Test
  # code...
end

Note. It is redundant to terminate the code instruction line with a semicolon (;) in IronRuby.

Final Words

This article has showcased the comprehensive process of IronRuby installation in the .NET framework in a step-by-step manner and also covered other essential properties configuration for IronRuby development environments. The First “Hello World” program has provided a basic overview of IronRuby, like how to code and compile Ruby programs in the .NET Framework context. Moreover, we have delved deeper by discussing the importance of the Dynamic Language Runtime (DLR) and mechanics behind the IronRuby open-source nature, which enables the execution of Ruby programs over any platform such as Mac, Windows, and Linux. At the end of the article, we have explored the fundamental constructs of the Ruby language such as declaration and initialization, data types, functions, and array constructs. In a few words, this article has explored the underlying techniques used to implement code generation and dynamic applications during the development, build, and runtime phases of an IronRuby application.

Reference

  1. https://github.com/ironlanguages
  2. http://ironruby.codeplex.com/releases
  3. http://www.techrepublic.com/
  4. http://msdn.microsoft.com/en-us/magazine/dd434651.aspx
  5. http://www.codethinked.com


Recommended Free Ebook
Similar Articles