One of the benefits of implementing a language on top
of .NET is that it enables developers using that language to get full
access to the rich framework libraries provided with the .NET
Framework.
For a simple example of this in action, I could create a
"HelloWPF.rb" text file and type in the following Ruby code below:
Figure 5
The above code uses the WPF UI framework to
create a Window that hosts a StackPanel layout manager that initially contains
just a button. When the button is pressed, a new label control is created
and added into the StackPanel (causing it to automatically be flowed in the
Window).
I can then run the above application using
IronRuby by passing the "HelloWPF.rb" text file as an argument to
rbx.exe:
Figure 6
When I run it I'll get a window with a WPF button (note
above I added a nice DropShadowBitmapEffect to it in the code above):
Figure 7
And each time I press the button a new label
will be added to the Window:
Figure 8
Not only does having the ability to use all of
the .NET APIs provide a lot of power, but you'll notice in the code we wrote
how it is possible to naturally integrate .NET APIs into other language
syntaxes:
Figure 9
In the code snippet above I'm using the Ruby block language
feature (similar to a Lambda expression with C# 3.0 and VB9) to implement a
"Click" event handler on the WPF button. Notice how within the
block the standard Ruby naming patterns can be used when accessing any .NET
API. For example, instead of using the "FontSize" property on
the WPF Label we are accessing it using "font_size" as the
property accessor name. IronRuby automatically handles the naming conversion -
enabling developers to program with a consistent naming pattern regardless of
their language of choice.