IronShay

Ironing code, geek t-shirts and even presentations!

NAVIGATION - SEARCH

IronRuby 0.3 Has Been Released!

Great news! As Jimmy Schementi describes it – the “conference-driven development schedule” of IronRuby brings us some news for the MIX09 conference. It’s the 0.3 version of IronRuby!

IronRuby 0.3 is out!

So what’s new in 0.3?

  1. IronRuby now passes 10% more of the RubySpec than v0.2 has. Make sure to check IronRuby.info in order to get up-to-date statistics.
  2. Better .Net interoperability – you can now call generic methods and implement interfaces!
  3. More! you can read about all changes in the CHANGELOG.txt file that you’ll find in the zip.

Grab it: http://rubyforge.org/frs/download.php/53552/ironruby-0.3.0.zip

Enjoy,
Shay.

Make Your Application Extendable Using the DLR

It’s very common for applications to have a way to extend them. Extensibility comes in various ways and have multiple names too – plug-ins, add-ins, addons, etc. It seems, though, that one kind of extensibility was left to the very few – application macros.
The concept is very simple – You don’t need to create a special Dll, implement a specific interface and register it somehow, you don’t even have to install an IDE. All you have to do is to open the relevant window, write some code and voila – you’ve extended the application.

What is this post about?

I’ll start with the result.
I built a simple application – it has a textbox, a “Send” button and a larger textbox for the application output. The flow is very simple – the user writes text in the textbox, hits “Send” and a “Hello” message appears in the larger textbox:

Extending Applications using the DLR - Sample #1

The power lies in the button on the bottom of the form - “Extensions”. Clicking there will open the extensions dialog, where you can write a macro which will be run after the user clicks “Send”. The users can choose the language for their macro, which will be one of the DLR’s languages – IronRuby or IronPython. After the user writes the code and hits “Save”, the application is extended!

Extending Applications using the DLR - Sample #2

 

Now when the user clicks send, look what happens:

Extending Applications using the DLR - Sample #3

This sample is very simple but it took me about 20 minutes to get it done. It’s just magic!

How was it done?

Well, all the credit goes to the DLR. This amazing little thing, makes it soooo easy to integrate dynamic languages into the .Net environment, that it’s a shame not to do so.

The heart of this application is the ExtensionRunner class and its Run method:


public static bool Run(Form1 frm, string code, bool rubyCode)

The method receives 3 parameters – the form object to pass to the executed code, the code that the user has written and a boolean value indicating whether it’s ruby or python code.

Firstly, I declare a ScriptScope and a ScriptEngine variables and fill them with the chosen language implementation. Because all DLR languages implement the same interfaces and classes, this is the only language-related code here. After that, the code will fit every DLR language. This means that adding IronScheme and Nua here, for instance, will be a matter of seconds:


ScriptScope
scope = null; ScriptEngine engine = null; if (rubyCode) { engine = Ruby.CreateEngine(); scope = Ruby.CreateRuntime().CreateScope(); } else { engine = Python.CreateEngine(); scope = Python.CreateRuntime().CreateScope(); }

Now we declare variables that the macro code will be able to use. We declare 2 variables:

  • frm – which is the form object. you wouldn’t want to do that in your application, the right way will be to pass a class with targeted functionality for the extensions.
  • return_value – the extension will put a value here. It’s a boolean value which indicates whether we should continue with the regular flow of the application after the extension has been executed or stop.

scope.SetVariable(
"frm",frm); scope.SetVariable("return_value", false);

The rest of the code is pretty straight forward – I create a ScriptSource object from the given code and execute it. Then I get the return_value value and return it to my main application.


// Run the code!
ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements); source.Execute(scope); // Get the return value bool retVal = scope.GetVariable<bool>("return_value"); return retVal;


This is it. The other code of the application is just a regular WinForms code so I won’t deep dive into it.

Conclusion

In conclusion, the DLR makes it very simple to run dynamic languages from your .Net code. I showed you here only one of the possibilities that are made available with this kind of integration.

Go ahead and try!

Download the source code

All the best,
Shay.

Like it? Share it: Loves soccer? Kick it  | Angry? Shout it  | In DA ZONE? DZone it

IronRuby, IronPython and DLR Daily Builds RSS Feed Is Available!

With the help of Harry Pierson (AKA DevHawk), there is an RSS feed that will notify you when a new nightly build is available!image 
The builds are daily and you get the latest versions of IronRuby, IronPython and the DLR in Silverlight or desktop versions.

So all of you early adapters, go ahead and subscribe to the feed!

Comment: the files in the feed are the built files, not the source code.

Enjoy,
Shay.

IronLanguages and Cross-Platformability

Most dynamic languages can run on multiple platforms. IronPython and IronRuby are no exception. The dev teams are investing lots of resources in order to make this happen because of the importance of this feature to the community.

IronPython and IronRuby can be run on top of Mono, which enables you to run .Net applications on top of different platforms, including Linux, BSD, Mac and more.

mono-gorilla

There are some great posts that show you how to do that:

Enjoy the cross-platformability! :)
Shay.

The dynamic Keyword Part 2 – IDynamicObject

This is the second part of my series about the new dynamic keyword in C# 4 (Read the first post here).

As I talked on the first post, the dynamic keyword does the dirty reflection work for you. This is very nice, but it’s like moving forward from a desktop computer to a 10kg laptop – you get to be more dynamic, but it’s still heavy to move around…

The first laptop - heavy!

The dynamic keyword holds a greater power inside, and this is what I’m going to talk about today – the IDynamicObject interface.

What is the IDynamicObject interface?

When we declare an object as dynamic and execute actions upon it, we actually use a default IDynamicObject implementation.

The IDynamicObject interface has a few methods, the important ones (for this post) are – Invoke, GetMember and SetMember.
The idea is very simple -

dynamic myObject = GetIDynamicObject();
myObject.SomeProperty = "Hello"; // This calls IDynamicObject.SetMember
Console.WriteLine(myObject.SomeProperty); // This calls IDynamicObject.GetMember
myObject.RunSomething(); // This calls IDynamicObject.Invoke

This means that we can implement our own behavior for the dynamic invocation! let’s see what we can do with that…

What can I do with that?

If we implement our own IDynamicObject, we can ease the pain of various daily operations. For example, we can develop an IDynamicObject that will give us the ability to use un-typed datasets as if they were typed. Another example can be an implementation that will enable us to dot through an xml file, with no need of XmlDocument, xpath and their friends. I’m sure there are tons of other uses that can and will be implemented.

Sample IDynamicObject implementation – Working with un-typed datasets

The first thing we’ll do is to create an extension method to the System.Data.DataSet class:

public static class Extensions
{
public static dynamic AsDynamic(this DataSet dataSet)
{
return new DataSetDynamicCaller(dataSet);
}
}
When we call DataSet.AsDynamic(), we’ll receive a DataSetDynamicCaller class in return, as a dynamic type object. 

Now what we need to do, is to generate classes that dynamically access the dataset objects. This is a very easy process as you will see in just a moment.

So let’s create the DataSetDynamicCaller class:

public class DataSetDynamicCaller : DynamicObject
{
DataSet dataset;

public DataSetDynamicCaller(DataSet dataset)
{
this.dataset = dataset;
}

public override object GetMember(System.Scripting.Actions.GetMemberAction info)
{
return new dataset.Tables[info.Name];
}
}

This is it… Now to the explanation…

Firstly, I inherit from DynamicObject – this is the basic implementation to the IDynamicObject interface (download link is at the end of the post). As I understand, the DynamicObject class will be a part of the System.Dynamic namespace in C# 4, so you won’t need this file on the final version. I won’t deep dive into this implementation, it’s a subject to another post.

Secondly, The DataSetDynamicCaller class is very simple – we get the untyped dataset in the constructor and save it in a private member. Then, whenever the user asks for a property, we return a DataTable that matches the name of the requested member.
This will give us the ability to call a table directly. For example:

DataSet ds = new DataSet();
ds.Tables.Add("MyTable");

dynamic dynDS = ds.AsDynamic();

DataTable tbl = dynDS.MyTable; // This will work!

It’s as simple as that!

I attach the class I���ve written (the link is at the end of the post). The sample contains two more classes and enables you to use the un-typed dataset even more easily. For instance, calling dynDS.MyTable.GetRow(0).MyColumn will return the value of the MyColumn column from the first row of the MyTable table…

In conclusion, the IDynamicObject interface opens a large amount of possibilities for improving our everyday work in a lot of aspects. It starts from accessing untyped datasets as if they were typed and ends where your imaginations does. This is what they mean when they say dynamic capabilities!

image

Downloads:
DynamicObject.cs
DynamicDataSet.cs

All the best,
Shay.

kick it on DotNetKicks.com
Shout it

IronRuby Code Repository Changes

Starting from today, you will not get new updates from the IronRuby SVN code repository. The repository has been moved to github, and SubVersion has been replaced with Git.

If you want the source code, you’d need to adjust your code with the new changes.

If you don’t need the code, only the builds, you can now download the latest 0.2 version as a zip file (that contains the needed DLLs and batch files). Soon enough you’ll be able to download daily builds as well – so the need for the source code reduces or even disappears for those who only wish to work with IronRuby.

The 0.2 version, the daily builds (coming soon) and the links to the Git repository can be found on the download page at: http://www.ironruby.net/Download.

All the best,
Shay.

IronRuby.net Was Recently Updated With a Brand New IronRuby Logo!

The IronRuby.net site (the official IronRuby site) has recently changed it layout from a kind of childish layout to a slicker one.  The new layout comes with a new IronRuby logo! here it is:

ironrubylogoIt’s a nice and simple logo – stays true to its roots :)

Some other updates to the site – the Roadmap page has been updated and it now contains the “must-have” and “nice-to-have” items for the first release of IronRuby. Quite an interesting list, worth a look.

 

All the best,
Shay.

IronRuby Tip: Ruby String and ClrString

When you use IronRuby to execute some C# code, for example, and the C# code returns a string, pay  IronRuby Tip: Ruby String and ClrStringattention that the string is not a Ruby string!
The type of the object you receive is ClrString. ClrString is the equivalent of System.String in IronRuby, all the System.String methods will work on ClrString objects, but Ruby’s string methods will not (and vice versa).

For example:

clr_string = MyCSharpNameSpace::MyClass.ReturnString()

clr_string.IndexOf("something") # This will work
clr_string.EndsWith("something") # This will work

clr_string[0..2] # This will NOT work
clr_string.index("something") # This will NOT work

As you can see, the System.String methods work but the Ruby string methods don’t.

What to do?

If you want to use a System.String as a Ruby string, all you have to do is call to_s! just like that:

clr_string = MyCSharpNameSpace::MyClass.ReturnString()

clr_string = clr_string.to_s # Converting the ClrString to a Ruby string

clr_string[0..2] # This will WORK!
clr_string.index("something") # This will WORK!

Just to make clear – you will not be able to use System.String methods after the conversion to a Ruby string… One language at a time, folks, one language at a time :)

All the best,
Shay.

The dynamic Keyword Part 1 - Introduction

Firstly, in order to use the dynamic keyword, you'll have to download the VS2010 VPC image and work there. There is no way currently, to download and install Visual Studio 2010 on your local computer.

Now we're ready to explore the future!

So what is the dynamic keyword?

The dynamic keyword is a new type of variable that will be added to the syntax of C# 4.0. Behind the scenes, the dynamic keyword is declared as an object with an attribute that indicates that this is a "special" object that should be treated dynamically.

The way you declare it is just like any other variable type:

dynamic myDynamicVariable = something;

What is it good for?

This dynamic mechanism is a wonderful thing for a bunch of different things that now we have trouble with. Things like - reflection in different environments, calling dynamic languages, dynamic fluent interfaces and more. All of these will become tremendously easier, using the dynamic keyword.

Example - Reflection using dynamic

Let's say I have a type that I define on one class library, I'll call it TheMysteriousType. This is its code:

namespace Mysterious
{
public class TheMysteriousType
{
public string RevealYourSecrets()
{
return "Never!";
}
}
}

Now, I'll create another class library with a method that receives a dynamic variable and executes the RevealYourSecrets method:

namespace Revealer
{
public class Revealer
{
public static void RevealSecrets(dynamic obj)
{
Console.WriteLine("Reveal you secrets, object! {0}", obj.RevealYourSecrets());
}
}
}

And last thing - I'll create a console application that has the above 2 class libraries references. It will contain the following code:

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Mysterious.TheMysteriousType myst = new Mysterious.TheMysteriousType();
Revealer.Revealer.RevealSecrets(myst);
}
}
}

The flow of the application goes like this:

  1. myst variable is created.
  2. Revealer.RevealSecrets gets the myst variable as dynamic.
  3. Inside Revealer.RevealSecrets: obj.RevelarYourSecrets() will generate a dynamic invocation of the RevealYourSecrets method. Behind the curtains, this will end up running the traditional reflection code for us.
  4. The application ends up writing to the screen: "Reveal your secrets, object! Never!".

This is a sample reflection scenario - the Revealer class does not know the object it receives. In order to invoke the RevealYourSecrets method prior to C# 4, we had to use some complicated reflection code. From C# 4, we'll have the dynamic keyword on our side that will do that for us.

This is great because it saves us time and make our code more readable, eventually making it easier to maintain.

Stay tuned to the next parts!
Happy Holidays!
Shay.

kick it on DotNetKicks.com

Getting Started With Dynamic Languages

I've grouped together some resources and blogs for all of you out there who are willing to start working with dynamic languages that are built on top of the DLR. Enjoy!

IronRuby IronRuby

Installation

Currently you'll have to get the code from the IronRuby SVN repository (svn://rubyforge.org/var/svn/ironruby  OR HTTP://ironruby.rubyforge.org/svn/trunk/) and build the project yourself.
Justin Etheredge has posted a step-by-step walk-through on his blog.

Resources

Recommended Blogs

IronPython IronPython

Installation

You can get the installer from the IronPython codeplex homepage: http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=8365

Resources

Recommended Blogs

IronScheme  IronScheme

 Installation

You can get the installer from: http://www.codeplex.com/IronScheme/Release/ProjectReleases.aspx?ReleaseId=14059

Resources

The Dynamic Language Runtime DLR - The Dynamic Language Runtime

There is no need to download the DLR code separately because it already comes with the installation of the other languages. If you want to write your own language on top of the DLR, this is the place to start for you.

Installation

You can get the binaries and the code from the codeplex homepage: http://www.codeplex.com/dlr/Release/ProjectReleases.aspx?ReleaseId=20378

Resources

Hope it helps,
Shay.

kick it on DotNetKicks.com