IronShay

Ironing code, geek t-shirts and even presentations!

NAVIGATION - SEARCH

IronRuby Tip: Data Binding Silverlight Elements to IronRuby Objects

When I was playing around with Silverlight and IronRuby, I ran into a problem with data binding and IronRuby objects. This post is to let you know about it and hand you the workaround as well.

The Problem

You have an IronRuby class. You fill it with data and set it as the data context of your Silverlight element.
The result: the values are not presented on the Silverlight page.

Detailed steps to reproduce:

1. Create an IronRuby class. For example:

class Person
  attr_accessor :full_name
  def initialize(full_name)
    self.full_name = full_name
  end
end


2. Create an instance of that class and set it as the data_context of an element:

shay = Person.new "Shay Friedman"
@root.data_context = shay


3. Set the element to present the data:

<StackPanel>
  <TextBlock>You name  is </TextBlock>
  <TextBlock Text="{Binding full_name}"/>
</StackPanel>


4. Run the page – nothing is shown.

 

The Reason

With the help of Jimmy Schementi I’ve figured out what the problem was. Silverlight 3.0 elements can only do reflection-based data binding. IronRuby objects are based on ICustomTypeDesciptor, which means that the attributes cannot be read using reflection. Bummer.

The Workaround

Instead of using an IronRuby class, use a CLR class. This means that in order to make it work, the work flow should be as follows:

1. Create a C#/VB.Net class. For example (assume this appears in “person.dll”):

class Person
{
  public string FullName { get; set; }
  public Person(string fullName)
  {
    FullName = fullName;
  }
}


2. Create an instance of that class in IronRuby and set it as the data_context of an element:

require "person.dll"
shay = Person.new("Shay Friedman")
@root.data_context = shay


3. Set the element to present the data:

<StackPanel>
  <TextBlock>You name  is </TextBlock>
  <TextBlock Text="{Binding FullName}"/>
</StackPanel>


4. Run the page – everything works as expected.

All the best,
Shay.

I'm Proud to Present - IronRuby Unleashed

Hi readers!

I'll start with the announcement - I'm writing a book! it is called "IronRuby Unleashed" and it will be published by Sams Publishing who has brought the world the great Unleashed series.

What is the Book About?

Well, I bet you've guessed it already - the book is about IronRuby. With version 1.0 right around the corner, it is more than important to have some reference books so developers can get started in no time.

The book is a ~500 page reference and guide book. You will start by learning the Ruby language (an essential part for IronRuby devs, I'd say) - from the basic to the advanced subjects. Then you will get to know IronRuby - how to use it in simple and complex occasions. On the third part of the book, you will go through different frameworks and learn how to take advantage of them with IronRuby (WinForms, WPF, Ruby on Rails, ASP.Net MVC and more). Eventually on the last part, you will be guided how to extend IronRuby and add your own touch to the language.

The table of contents (subject to change):

  • Part 1 - Introduction to IronRuby
    • The Ruby Language
    • The .Net Framework
    • The DLR
    • Getting Started with IronRuby
  • Part 2 - The Ruby Language
    • The Basic Basics
    • Methods, Blocks, Classes and Modules
    • The Standard Library
    • Advanced Ruby
  • Part 3 - IronRuby Fundamentals
    • .Net Interoperability Fundamentals
    • Object Oriented .Net in IronRuby
  • Part 4 - IronRuby and the .Net World
    • Data Access
    • Win Forms
    • WPF
    • ASP.Net MVC
    • Ruby on Rails
    • Silverlight
    • Unit Testing
    • Using IronRuby from C#/VB.Net
  • Part 5 - Advanced IronRuby
    • Extending IronRuby using C#/VB.Net

Who is it for?

I tried to write the book in a way developers from both sides, Ruby and .Net, can benefit from it. In addition to that, because of the structure of the book (basics to advanced), people who want to learn their first programming language might find the book helpful as well.
Generally, If you are interested in IronRuby, I believe you will find what you're looking for in this book.

Why Did I Do It?

Hmmm... Tough one... firstly, I really believe in IronRuby. I think it is a killer language which gives developers so much power with such a small effort. For me it was something I had been waiting for for a long time - a language that is fun to work with but still is powerful. By the time I started writing the book I didn't even know how big of a fan I would become...
Secondly, I wanted to know how it would be like to write a programming book (hint: not easy at all!).
Thirdly, I needed something to make mom and dad proud :-)

Where Can I Read Sample Chapters (and purchase purchase purchase!)?

I'm glad you asked. The book is already on Amazon and the Safari Books Online ready for pre-ordering.

The book is also on the Rough Cuts program, which means that a few chapters are released while they are in development so you readers can get a sneak peek, express your opinions and help make the book better. You can access the chapters via the Safari Books web site.

Well, that's about it. I'm still working on it, but the finish line is nearby.
Hope you'll enjoy it,
Shay.

Do You Want IronRuby/IronPython Support in Visual Studio 2010?

I know I do. If you want too, please rate and vote up the suggestions on the Microsoft Connect site:

IronRuby VS Integration: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=479957

IronPython VS Integration: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=475830

Yes we can!
Shay.

WinForms in IronRuby

IronRuby holds so much potential. I can say from my perspective, that since I’ve been playing around with it, I became more and more enthusiastic about it and sure that it’s going to succeed big time.

However, IronRuby still has a few drawbacks. They focus mainly on the the lack of Visual Studio support and its side-effects. WinForms is one of the technologies that suffer from this shortcoming. Do you expect me to develop WinForms without the precious designer? I know everyone has their love-hate relationship with the VS designer, but taking it away entirely is just too much to handle.

Well, that’s what I’m here for - to hand you a solution!

I’ve written an IronRuby script (actually, it will run on MRI as well) that will convert a winform designer code (C# only currently) to IronRuby code. By using the script, you can use the VS designer and create the form you want. After you’re finished, run the script and voila – you have all the designer code in IronRuby code!

I won’t explain the code here (maybe I’ll write another post about it, not sure) but in short, it uses regular expressions to find several C# things and replaces them to ruby things…

The file

The ruby source file can be downloaded from here.

(if someone is willing to help me uploading it to GitHub, please do :) ).

How to use it?

First, you need to load the ruby library. The file is named “win_forms_converter.rb” so just start by requiring it:

require “win_forms_converter.rb” # Assuming it appears within the current directory

The library contains one single method accessible from WinFormsConverter.convert_winform. The method receives three parameters:

  • designer_file – the path to the form designer file. For example, C:\Dev\WinForm\Form1.designer.cs.
  • target_file – the path of the new IronRuby file. For example, C:\Dev\IronRubyWinForm\Form1.rb.
  • target_main_file – optional parameter. The path of the form runner file. If this parameter exists, a file will be created on the given path that executes the created form. For example, C:\Dev\IronRubyWinForm\main.rb.

REPL Example

This is a sample command prompt session for converting a WinForm to IronRuby and run it:

______________________

C:\>d:

D:\>cd demo

D:\Demo>ir
IronRuby 0.5.0.0 on .NET 2.0.50727.4016
Copyright (c) Microsoft Corporation. All rights reserved.

>>> require  "win_forms_converter.rb"
=> true
>>> WinFormsConverter.convert_winform('Form1.designer.cs','D:\Demo\IR\form1.rb','D:\Demo\IR\main.rb')
Converting designer file from Form1.designer.cs
Done.
Creating IronRuby file at D:\Demo\IR\form1.rb
Done
Creating main file at D:\Demo\IR\main.rb
Done
Conversion is finished.
=> nil
>>> exit

D:\Demo>cd IR

D:\Demo\IR>ir main.rb

_______________________

Pay ATTENTION

This is the very very first version of this convertor. I haven’t tested it deeply and I’m kind of freeing it to the wild so you people can take it and make it much better.

If you need any explanation about the source  code, you have comments, you’ve found bugs or whatever, you’re welcome to contact me via the contact page or via twitter @ironshay.

Hope it helps,
All the best,
Shay.

New IronRuby and IronPython Versions

Good news! IronRuby and IronPython have just been released with new and shiny versions!

IronRuby 0.5

  • Download IronRuby 0.5
  • Adaptive compilation – performance boost!
  • Removes ClrString. CLR strings are referred to as System::String
  • Removes IronRuby library. "require 'IronRuby'" is no longer needed; IronRuby module is now a built-in module.
  • Integer/Float operations for all CLR primitive numeric types
  • System::Char and System::String methods so that they behave like an immutable UTF-8 encoded string
  • Improves handling of BOM in 1.8 mode
  • Debugger visualizer for RubyObject so that VS will display the result of to_s in the Watch window.
  • Adds support for .NET operators (op_* special methods)
  • Implements bitwise operators on flag enums
  • Precompilation of simple Ruby method invocations
  • Improves performance for file IO#read method. Previously we were 10x slower than MRI, now we are approx. 1.5-times faster.
  • Tons of fixes to File, String, Array, and other built-in types to drive up RubySpec and Ruby application test-suite pass-rates. One bug fix is mine!
  • For a detailed description, read Jimmy’s blog post: http://blog.jimmy.schementi.com/2009/05/ironruby-05-released.html

IronPython 2.6 Beta 1

.Net 4.0 CTPs

With the release of VS2010, new IronLanguages CTPs came along. These CTPs make calling dynamic languages from C# 4.0 a very easy task. Give it a try:

IronPython 2.6 CTP for .Net 4.0 Beta 1

IronRuby CTP for .NET 4.0 Beta 1

 

We’re living in exciting times guys!

All the best,
Shay.

kick it on DotNetKicks.com  Shout it

IronRuby Journey Update #1 – Establishing The Silverlight Environment for IronRuby

OK, so I’ve started up my project to port a Silverlight application from C# to IronRuby. The special thing about this app is that it also makes some webservice calls, something that isn’t as obvious as it is in C# - you don’t have the wsdl tool to create the proxy class for you.

Well, Ruby is a fun language to code in so I’m sure that the lack of a few things here and there will not be much of a problem (I’m sure some of you nod your heads with a “yea right” look on your faces :) ).

Step #1

The first thing I needed to do was to be able to use IronRuby with Silverlight. This is done by the agDLR project which is a pack of tools to help you integrate Silverlight and the DLR. agDLR version is currently 0.5 which means it’s still a beta version and hopefully will improve until V1. It currently supports Silverlight 2 and Silverlight 3 beta.

I downloaded it – first task is done!

Step #2

Next I needed to create an IronRuby-driven Silverlight application. Among the agDLR files, there is a tool named SL. This is a command line tool that generates a simple Silverlight application with code files written in the selected dynamic language (Ruby and Python are supported). You will find it under the scripts folder where you extracted the agDLR files. The command line parameters are simple:

sl [ruby|python] <ApplicationPath>

Running it looks like that:

Running agDLR SL tool

After the tool is done, it generates a folder with the needed files to start with:

  • D:\IronRuby\WeatherWidget
    • \app
      • app.rb
      • app.xaml
    • \css
      • screen.css
    • \js
      • error.js
    • index.html

I have the project files ready – step #2 is complete!

Step #3

Trying to browse to the index.html file that sl has generated for us will not take us to where we want. We  will simply hit an error telling us that the silverlight file could not be downloaded. This is because the index.html file looks for app.xap, which wasn’t generated by the sl tool.

This is when the Chiron tool comes to our aid.

In the Greek mythology, Chiron was a very wise and kind centaur (a man-horse combination) and the teacher of many heroes like Asclepius the God of medicine, Theseus the legendary king of Athens and the greatest warrior Achilles.

In 2009, Chiron is a local web server which runs directly from the command line. It dynamically generates the silverlight xap file from your dynamic language code. You can run it by using the server.bat file that is located under the scripts folder. Chiron has various different startup options which you can view if you execute it with the /? switch: server /?.

In order to run the Silverlight project, you need to run server /b from the project directory (which means you need it on your PATH list) or by passing the directory using the /d switch. I went on and created a batch file to start the server automatically from the project directory, the batch file has a single line inside:

d:\IronRuby\agdlr-0.5.0\script\server /b /d:d:\IronRuby\WeatherWidget

So now all I need to do is run my batch file! Running it starts the web server on port 2060 and opens the browser with the directory listing:

Browser after running Chiron

 

Looking at the command line, you can see what chiron has fetched:

Chiron running

Clicking on the index.html file will show you the default Silverlight application that sl has generated for us. It has a built in console where you can write IronRuby code and see the changes live (awesome!):

Default agDLR silverlight application

OK – the environment is ready! step 3 is done.

Update #1 Conclusion

Well, I’ve got to say, this wasn’t easy. I think that this process of establishing the Silverlight environment revealed the Achilles heel of the current status of Silverlight and DLR languages. The process is not even nearly as intuitive and fluent as the C# process from Visual Studio (this whole post would have been one line if it was a C# tutorial – choose New->Silverlight Application and click OK).

.Net programmers have gotten used to intuitive, rich and fluent work process which obviously will not be what they’ll get with DLR languages currently.

I guess that these issues will improve with time. Currently, Microsoft spends time on the core things and not on the nice-to-have things. After IronRuby, IronPython and the DLR core development is done, Visual Studio integration will follow.

For me, I’m not planning on giving up. I really believe in these languages and the great things they bring to the programming world.

Stay tuned!

All the best,
Shay.

Share: DotNetKicks | DotNetShoutout | DZone

IronRuby Journey Update #0

Well, the journey is going very slow… But I wanted to let you know that it’s on!

This is update #0 and not #1 because it’s an update. I’ve decided to help the community with this journey, so instead of writing VS integration, I’ll be writing a Silverlight weather widget that uses web services for fetching data.

I even appear on the IronRuby Wiki as a contributor:

I'm an IronRuby Contributor! Woohoo!

(From: http://wiki.github.com/ironruby/ironruby/contributing)

I have already started and I have some insights about doing Silverlight stuff with Iron languages. This is going to be the content of update #1.

Stay tuned,
Shay.

An Invitation To My IronRuby Journey

I’ve just realized that I’ve been preaching about IronRuby and the DLR - telling you to use it, showing you how, but I’ve never deep dived into it by myself. So I’ve decided to go deeper and examine IronRuby in “real life” situations. I want to run into its rough edges, write workarounds, feel the engine.

How far are we from V1? Is 0.3 really 70% far from being complete? from being ready to use?
I’m planning to find out the answers in the tough way – by getting my hands dirty!

IronRyb Journey You’re probably wondering how I’m gonna do that. Well, it’s simple – I’m going to take ideas and develop them entirely in IronRuby. I will write here about my experience and share my thoughts, critic, successes and failures.

My first idea I’m going to work on is an ironic one – An IronRuby Visual Studio Integration.
The target is to achieve a simple code coloring ability and an auto-completion mechanism for IronRuby inside VS.

If you have more ideas for the rest of the journey, please let me know!

Off we go!

Shay.

Getting Started with IronRuby

The web is filled with lots of resources about Ruby and .Net. It does lack of resources about the combination – IronRuby. I’m sure this will get better as time goes by and official releases are out the door.

Here I’ll try to gather the most effective resources for you so your entrance to the IronRuby world will be smooth and enjoyable.

IronRuby resources

  • IronRuby.net – the official site and a great place to start.
  • IronRuby Mailing List – THE place to ask questions (look here if you prefer to watch it in a forum view).
  • GitHub site – IronRuby code repository on GitHub.
  • GitHub wiki - How to get, build, debug and contribute to the IronRuby source.

IronRuby Related Blogs

For .Net developers – Ruby resources

For Ruby developers - .Net resources

If I have missed something, please comment or contact me and I’ll add it!

Enjoy IronRubying!
Shay.

Share: DotNetKicks | DotNetShoutOut | DZone

Tip: How To Use Generic .Net Classes in IronRuby

In the latest version of IronRuby (0.3), the ability to use Generic .Net classes was added. I couldn’t find anywhere how to do that so I dug it out of the code and now I’ll share it with you!

Example #1 - List

This is how to define an Int32 list, add two numbers to it and print them:

list = System::Collections::Generic::List[System::Int32].new

list.add 4
list.add 12

list.each { |x| puts x }

Of course we can also use Ruby type like Numeric, String, etc.

Example #2 – Dictionary

This is how to declare a number-string dictionary, add values to it and print them:

dict = System::Collections::Generic::Dictionary[Fixnum, String].new

dict.add 1, "Hey"
dict.add 15, "There"

dict.each { |x| puts "#{x.key} - #{x.value}" }

 

All the best,
Shay.