- Posted by Shay Friedman on October 29, 2009
I started to wonder about that when I was looking for an equivalent method in Ruby. Apparently, Ruby doesn’t come with such a method built-in, but you can add it very easily by using Ruyb’s monkey patching abilities.
This is odd, because Ruby is the greatest language and has everything you possibly need (and IronRuby is even better! :) ). So why isn’t there an IsNullOrEmpty-like method? Well, they might just didn’t think it was important enough. And there might be a different answer, maybe the decision has other reasons below the surface.
Several developers have discussed the String.IsNullOrEmpty issue over the years. Some say it’s good, some say it’s bad and some even say it’s hazardous to your code (because of a bug it has).
For me, I go with Ruby’s approach (unless they didn’t add it because of time constraints). Most of the time there will be a big difference between a null string object and an empty string object. Therefore, I think that String.IsNullOrEmpty() should be avoided as much as possible and the application should have a convention of how uninitialized strings should look like – nulls or String.Empty.
For example, say you need to validate some input and String.Empty is sent to you. How do you know if the string is an uninitialized string or a real input? saying that null is the convention would have solved this confusion.
I know there are cases where you have to use IsNullOrEmpty. For example, when using 3rd party components that you are not aware of their uninitialized string convention (if such exists…). This is why I don’t rule out IsNullOrEmpty entirely. I do say that wherever you can, do not use it and prefer str == null to spot uninitialized strings.
By the way, deciding that only null is the convention for uninitialized strings can also improve your application performance since str == null is faster than String.IsNullOrEmpty(str) in about 35%. However, you’ll notice an improvement only if you’re doing billions of String.IsNullOrEmpty calls, so don’t panic right away.
In conclusion, String.IsNullOrEmpty is there for convenience reasons. Sometimes with convenience comes tranquility, so make use of it wisely!
Just a final addition, Ruby on Rails adds a blank? method to Ruby which provides the equivalent to String.IsNullOrEmpty method in C#. So, if Ruby does not contain everything you need, Ruby on Rails surely does! :)
So what do you think, IsNullOrEmpty is good or bad for your application?
All the best,
- Posted by Shay Friedman on October 26, 2009
[This post is the second in my series of IronRuby samples. Read the first one here]
The release of Visual Studio 2010 Beta 2 and IronRuby .Net 4.0 Beta 2 CTP has brought some AMAZING abilities to the .Net world like the dynamic keyword. This keyword is a revolutionary little thing. It takes everything you know about C# and throws it away – explicit types, locating syntax errors in compilation time, compiled code…
Sounds bad? well, it is just AWESOME!!! The dynamic keyword brings so much goodness to our beloved C# language, that if it was possible I would have hugged it and asked it to join my family.
Well, enough with the nonsense, let’s get down to business. IronRuby is Microsoft’s implementation of the Ruby language. It runs on top of the DLR and provides a seamless integration with .Net code. In short, it ROCKZZZZZ. This post is about IronRuby’s seamless integration with .Net and the ability to use the great power of Ruby inside C#.
The Ruby language has some very powerful metaprogramming abilities. One of those is the method_missing method. When you declare it in your class, every call to a method that doesn’t exist will be routed to it. You can then do whatever you want with the call – execute a different method, raise an exception, interpret the call somehow or just do whatever you want (jump in the air? do your little Irish dance thing?).
Another nice metaprogramming feature is the ability to send requests to objects by using the send method. The concept is very similar to C#’s reflection method – Invoke.
Now if we combine method_missing and send, we can create a class that saves calls and playbacks them upon request. I will call it… tam tam tam… Recorder:
# Initialize an array that will save the calls
@calls = 
# Save the calls to method_missing
def method_missing(name, *args, &block)
@calls << [name, args, block]
# Playback the calls on a given object
@calls.each do |name, args, block|
obj.send name, *args, &block
I think this code is pretty straight forward, no special things here. With this class defined, we can record Ruby calls and playback them on Ruby objects:
# Record calls
rec = Recorder.new
rec.insert 2, "ABAB"
# Playback them on a real object
str = "Hello World"
puts str # Prints "dlBBroW olleH"
It is AWESOME, but the great thing about it is that with .Net 4.0 and the dynamic keyword, it is available in C# too!
To try the next code by yourself, first open Visual Studio 2010 Beta 2, create a new C# console application and add references to IronRuby.dll, IronRuby.Libraries.dll, Microsoft.Scripting.dll and Microsoft.Dynamic.dll (remember to use the CTP assemblies and not the regular IronRuby assemblies).
The following code loads the Ruby recorder class file (recorder.rb) to the C# environment, creates an instance of the Recorder class, records a few operations and playbacks them on .Net objects:
static void Main(string args)
// Load the recorder IronRuby file
var engine = IronRuby.Ruby.CreateEngine();
dynamic ruby = engine.Runtime.Globals;
// Initialize IronRuby's recorder class
dynamic recorder = ruby.Recorder.@new();
// Playback on CLR's List object
List<int> list = new List<int>();
// Print the results!
foreach (var item in list)
// Record console printing
recorder = ruby.Recorder.@new();
recorder.WriteLine(" and .Net 4.0");
// Playback on console
The output to the console will be:
IronRuby and .Net 4.0
Try it out and see the magic happens right in front of your very own eyes!
In my opinion, this joint venture is incredibly helpful and useful. I predict that as time goes by we will see more and more dynamic language code make its way to the conservative .Net world, enhancing it and adding it powerful abilities that it never has had before.
All the best,
- Posted by Shay Friedman on October 22, 2009
A bunch of readers have asked me to post IronRuby samples. I took your advice and I am starting a series of posts where I’ll write some IronRuby samples so you can see what’s IronRuby code is all about.
I’ll begin with the simplest sample and as the series goes on, I’ll try to bring you more complex samples. If you’d like to see a specific sample, please contact me and let me know about it.
So the first sample is a Hello World sample. The regular Ruby Hello World app, which of course runs on IronRuby as well, is as follows:
Now, the great thing about IronRuby is the ability to use .Net classes. The next code also writes Hello World to the console, but this time it uses .Net’s System.Console class. The sample also sets the color of the text to green:
orig_color = System::Console.foreground_color
System::Console.foreground_color = System::ConsoleColor.green
System::Console.write_line "Hello World"
System::Console.foreground_color = orig_color
To run that, just save this text into a file and execute it by ir.exe <file name>. This is how its execution will look like:
.Net developers pay attention - CLR objects get Ruby’s naming conventions when they are converted to IronRuby objects. This operation is called “name mangling”. This is why System.Console.ForegroundColor appears as System::Console.foreground_color in IronRuby. You can read more about name mangling in the .Net Interoperability Fundamentals chapter in my book. These chapters are available for free through the Rough Cuts program.
If you’d like to see a certain sample next time, let me know.
All the best,
- Posted by Shay Friedman on October 22, 2009
I’m glad to announce that another IronRuby session is getting closer, this time at the Israeli Web Developers Community!
Sunday, 1st of November, 2009. 17:00-20:00.
Microsoft Offices, 2 HaPnina St., Rannana.
The session is all about IronRuby, with a focus on Ruby’s most famous member – Ruby on Rails. The session will start with an overview of the Ruby language and its powerful abilities, continue with IronRuby and the stuff it brings to your everyday work and end with a Ruby On Rails overview, including live demo of building a Web 2.0 application from scratch.
Because IronRuby can enhance your everyday work, speed up your development process and make it enjoyable and fun! In addition, you will see another web development framework, Ruby on Rails, which will show you another way of web development and will also give you an idea about where today’s web MVC frameworks got all their ideas and probably their future ones as well :)
And most importantly, there will be free food and drinks!
Register to the event at: http://msevents.microsoft.com/cui/EventDetail.aspx?EventID=1032426828&culture=he-IL
By the way, If you’d like me to come and present at your event (conference, user group, internal gatherings, courses…), please contact me.
Hope to see you there,
- Posted by Shay Friedman on October 15, 2009
There are times when we need to use different code statements for different build configurations – this means that some code will not exist in assemblies that are built in certain build configurations.
Compilation symbols come to help in this case. You can set a symbol that will exist in a specific build configuration and then use it inside your code files to write or exclude code when this project is built using this build configuration.
The most familiar case of this scenario is debug and release build configurations. Actually, the debug build configurations comes with a compilation symbol by default named DEBUG. By using it you can write code that will run only when you compile your project in debug mode (show extra message boxes, write more logs, etc.).
Creating Compilation Symbols
- In Visual Studio, go to Project-> <project name> Properties…
- The project properties view will open. Click on “Build” on the left.
- The Build settings will open on the right. The first field there, with the label “Conditional compilation symbols” is the one you need.
- Add there the name or names (semi-colon separated) of the compilation symbols you want to define. Pay attention that the naming convention for these symbols is all uppercase.
You’re done. You can start using the compilation symbols you have just defined. The next screenshot contains a sample project with two symbols defined – SHAY and FRIEDMAN:
Using Compilation Symbols
In order to use the compilation symbols you have defined, you need to use the special compilation conditions. If these conditions are not met, the code within the condition will not be compiled at all.
The condition starts with #if <condition> and ends with #endif. For example, the next code will be compiled only when SHAY is defined (which is in debug build configuration):
Console.WriteLine("SHAY is defined! woot!");
You can also write more complicated conditions like !SHAY for all build configuration without SHAY defined. You can add conditions too – SHAY && FRIEDMAN.
Well, that’s about it!
Hope it helps,
- Posted by Shay Friedman on October 14, 2009
Yesterday I did a presentation at the Nes Ziona user group. My session was about IronRuby and my main goal was to show the audience what they can do with IronRuby in their everyday .Net work.
First of all, thanks everyone for coming! I had a lot of fun!
I discussed the following subjects:
- Creating internal tools and POCs with IronRuby – taking advantage of the Ruby language abilities and libraries to write code faster.
- Using Ruby REPL abilities in .Net apps – can be used for polishing, debugging and extending .Net applications.
- Cucumber – the amazing Ruby test framework that now can help test .Net code.
- Gestalt – oh yea!
You can download all the demos from here: NesZionaUGDemos.zip (4.82Mb)
I didn’t have any slides, so no ppt this time!
Some resources to get you started:
If you have any questions, suggestions, complaints, consulting needs or whatever, don’t hesitate to contact me (also via twitter)!
Thanks for attending!
- Posted by Shay Friedman on October 9, 2009
It’s time for another session about IronRuby!
The Nes Ziona user group, HP Indigo Offices – Scientific Park – 20 Einstein St. Nes Ziona.
Tuesday, 13-October-2009, 17:00 – 20:15
The session will be separated into two parts – the first one will be passed by Vitaly Kushner who works with Ruby everyday in the company Astrails. Vitaly will introduce Ruby to you – syntax, Ruby on Rails and general coolness. On the second part (starting from 19:00) I will take everything Vitaly has told you about and bring it to the .Net world. I will introduce you IronRuby (shortly) and then move forward to explain you how you can use it in your everyday work in order to enhance yours and your team development life.
Because IronRuby rocks and I really believe it can help any one of you in your everyday work.
Register at: http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032429242&culture=he-IL
there’ll be food and drinks for free!!!
Hope to see you there!
- Posted by Shay Friedman on October 8, 2009
Everywhere I go and preach about IronRuby I come across .Net devs who don’t understand what’s in it for them. This time I’ve decided to take action and tell you all about what’s in it for you and why you should be as excited as I am about the upcoming IronRuby 1.0.
This post is written as a conversation between me and my imaginary .Net developer friend (don’t you have one too?). Enjoy!
Friend: Hey Shay, what is this IronRuby that you can’t stop talking about?
Me: Oh imaginary friend, finally you’re being productive! well, IronRuby is Microsoft’s implementation of the Ruby language which runs on top of the DLR. Its unique power, comparing to other Ruby implementations (MRI, JRuby…), is its ability to interop with .Net assemblies with ease.
Friend: So is it an entire new language?
Me: Ruby isn’t a new language, it’s been around since 1996. However, it is a new language for the .Net framework. It is the first time for Ruby to be a fully .Net language. As a result of IronRuby being a .Net language, just like you can run VB.Net from C# and C# from VB.Net, you can run C# from IronRuby and vice versa.
Friend: Why did Microsoft choose Ruby from all the languages out there?
Me: First of all, Ruby rocks! secondly, along with the development of the Dynamic Language Runtime (DLR), Microsoft decided to develop .Net implementations of Ruby and Python. These languages appear in the top 10 of programming languages (look here) and with these .Net implementations, Microsoft will open its doors to thousands of developers worldwide, who until now had no reason to take a look at the .Net framework.
Friend: What’s in it for Ruby developers? Why would they want to use IronRuby?
Me: For Ruby devs, using IronRuby means that they can use .Net framework frameworks like WCF, WPF, ASP.Net MVC, Silverlight and others. Even though there are many libraries and frameworks available in Ruby, Microsoft does have some killer frameworks which are hard to find elsewhere like Silverlight. Another advantage is the possibility to use Microsoft infrastructure. For example, running Ruby on Rails on IIS.
Friend: Ok, so if I want to use IronRuby, I need to learn Ruby, right?
Friend: But I don’t know the language!
Me: Just like many other .Net developers, you will need to learn the Ruby language in order to write IronRuby code. Do NOT be afraid. Ruby is an easy language to learn and I promise you that learning it will open your eyes to a whole new world (which might amaze you, beware!). When I was learning Ruby I was surprised time after time about its syntax and the fun feeling you have while coding. Moreover, learning another programming language is a good practice to increase your knowledge of programming in general (read my other post about how to increase code quality).
Friend: Assuming I have learned Ruby, the company I work for will never abandon C#/VB.Net for IronRuby.
Me: I agree, I don’t believe companies will stop using C# and start coding in IronRuby. It isn’t gonna happen. However, when companies realize the benefits of using IronRuby in different scenarios, you will start seeing IronRuby code over there.
Friend: How can a company benefit from using IronRuby?
Me: A lot. As I said, I don’t expect companies to change their core code to IronRuby. There’s no reason to do that. But, there are several other areas where you can use IronRuby – tests, POCs, automation, internal tools and more.
Friend: How can IronRuby improve my tests?
Me: IronRuby has incredible test frameworks. For me, Cucumber is a killer test framework. It’s one of the most brilliant frameworks I’ve run into. To make it possible for QA and product managers to write specs in a human-readable syntax and run them as tests afterwards is just astonishing. Ruby has other great and widely-used test frameworks like RSpec and even web test frameworks like Watir.
Friend: Sounds pretty promising I must say. And why did you say it was good for POCs?
Me: Ruby is a language that enables you to write code fast. This is why writing a POC in IronRuby will take less time to develop. Remember that when you develop in IronRuby, everything is a CLR object so it is almost like writing C#/VB.Net code. Once the IronRuby POC is done and the feature goes into production development, transferring the IronRuby code to C#/VB.Net is very easy (SharpDeveloper even has a feature that does the conversion automatically). Same goes for internal tools – you write the tools faster and continue with your other tasks (look at my first ever IronRuby post where I was excited how easy and fast it was to write a tool I needed) . By the way, writing in a different language from time to time can also be a breeze of fresh air and a great routine breaker.
Friend: Great! and what about automation?
Me: Ruby has Rake which is similar to Nant in concept, just uses Ruby syntax. Once you know Ruby, there is not need to learn Nant as well… just use Rake.
Friend: You make faces like there is more. Is there?
Me: Yes! Except all the great Ruby stuff, you can also use IronRuby from your static language code. This is great because it lets you extend your application in a very easy way (take a look at my post about the subject). Another thing, the DLR along with Silverlight gives client scripting a whole new life and makes it bearable (which is not an easy task at all). Take a look at the amazing Gestalt project for more about that.
Friend: Now let’s say I write something in IronRuby. How do I deploy it? is it hard?
Me: The deployment is a matter of seconds. It is actually just copy and paste. All you need in order to run IronRuby on a different machine is to copy the Dlls there.
Friend: Nice! Well, I’ve waited till now and here is the million dollar question – can I write IronRuby inside Visual Studio?
Me: Well, in the meantime - no. There is a voting going on to convince Microsoft to do that ASAP. However, there are some great Ruby IDEs (like NetBeans, RubyMine, SciTE and more) that you can use. There is also the Ruby-in-Steel Visual Studio addin which makes it possible to work on Ruby files inside Visual Studio (it doesn’t work with IronRuby yet, but it is included in their plans). These IDEs do not have the intellisense that you are used to in VS and C#, but they are good for the job. I must say, once you get familiar with the language, you don’t need intellisense anymore. Really.
Friend: You got me convinced! I want to start IronRubying!!!
Me: Awesome! You can start with http://IronRuby.net and download the latest IronRuby version from there. If you have any questions, make sure to check the IronRuby mailing list or contact me and I’ll do my best to help. By the way, IronRuby is currently in version 0.9.1 and will advance to version 1.0 in the next couple of months.
Friend: Thanks Shay! now I see what IronRuby gives me and I’m willing to give it a try!
Me: Glad I could help! see ya!
Readers, do you have any questions? post them as comments and I’ll address them as soon as I can.
- Posted by Shay Friedman on October 5, 2009
A while ago I wrote a post about using Generic CLR classes from IronRuby. This time I’ll share with you its less intuitive friend – using generic CLR methods from IronRuby.
As a result of Ruby being a duck-typed language which works with types implicitly, generics is not really needed. The whole language is generic… This is why using generic CLR types might become a bit odd for the language. Nonetheless, this fact will not stop us. The price to pay in order to enjoy Ruby in the .Net framework is worth it! :)
Anyway, assuming we have the following C# class defined in file sample.dll:
public class Class1
public string Test<T>(T param)
In order to use it with a specific type, you need to do two things – get the generic method object (by using the method method) and provide it with the type via the special of method. For example, the next code invokes Test with a string and pass it “Shay Friedman”:
sample = ClassLibrary1::Class1.new
sample.method(:test).of(String).call("Shay Friedman") # returns "Shay Friedman"
And that’s all there is to know.
All the best,
- Posted by Shay Friedman on October 1, 2009
I talked with Ariel today and recommended him my favorite Ruby editor for small scripts and apps – SciTE.
The nice thing about SciTE is the ability to run your script file from the application via the F5 key. Since we were talking about IronRuby, SciTE gave us good syntax highlighting and auto-completion features, but we couldn’t execute our code on the IronRuby interpreter.
BUT… this is a problem of the past now! with a few simple steps, you can turn your SciTE environment to an IronRuby stupendous development environment! or at least make it use the IronRuby interpreter…
Now, to the job:
- Open SciTE.
- Go to Options->Open ruby.properties:
- A file with a lot of settings will be opened inside the editor window. Look for the text “ruby $(FileNameExt)” (I have it on line 108 and 116).
- Change “ruby $(FileNameExt)” to “ir $(FileNameExt)” on both places (if you’re on Windows, changing the first one will be enough):
Pay attention that there is the rbw line (look at line 110 on the image). I haven’t changed it because it’s not that common to use rbw files and also IronRuby doesn’t have the option as far as I know… I just left it there to use the regular MRI interpreter.
If you want to make sure that every Ruby file on SciTE environment is run via IronRuby, change “rubyw $(FileNameExt)” to “ir $(FileNameExt)” as well.
You must have ir.exe defined in your PATH environment variable in order for this to work. It is a good practice to do that, but if you don’t want to, just write the full path to ir.exe instead of just “ir”.
- Save the file.
That’s it, your’re done! now go and create an rb file and hit F5. IronRuby will run your script now.