6

I am giving a series of talks to a .NET (C#) development team on the Ruby language and environment. I am approaching it as an opportunity to highlight the benefits of Ruby over C#. At first, I want to concentrate on the language itself before moving into the environment (RoR vs ASP MVC, etc). What features of the Ruby language would you cover?

2
  • The culture of SO is such that we don't like language wars. "Benefits of Ruby over C#" is liable to get some folks worked up. I suggest an edit to be a little less subjective and argumentative.
    – Robert S.
    Feb 23, 2009 at 22:30
  • I am not trying to start a language war, however I do want to get various answers as to what makes one language (ruby) different from another (C#).
    – jsmorris
    Feb 24, 2009 at 21:54

5 Answers 5

10

I gave a talk to a .NET user group a while ago about IronRuby, and faced similar problems. The things I focused on were:

  • Duck typing. There's nothing stupider than List<string> stringList = new List<string>();

  • Expressive and Concise syntax. Simple things like leaving out parentheses, array and hash literals, etc. (combined with duck typing, you get string_list = [] which is obviously nicer). All small things which add up in a big way.

  • Metaprogramming. Starting with simple things like attr_accessor, then maybe something a bit more advanced if they don't immediately see the benefits. Don't try and compare things to lisp and wax on about programs writing other programs... people will just think you are smoking something. Stay simple and hammer home the point that you don't have to keep writing the same crappy boilerplate code anymore

  • As a good "finale", show them some normal NUnit style tests with all the mess of Assert.NotEqual<string> blah that they usually have, then say "here's the same code written in ruby" and show them it written using rspec (it will be half the length and 10 times easier to read... if that doesn't sell them, nothing will).

5
  • There's nothing stupider than List<string> stringList = new List<string>(); <-- thats not duck typing, that's type inference.
    – MichaelGG
    Feb 23, 2009 at 23:27
  • I mean, getting around that is inference, i.e., (var x = new List<string>()). And +1 for more literals and metaprogramming.
    – MichaelGG
    Feb 23, 2009 at 23:28
  • Sure type inference is great, but it's still limited to local variables. I have many class definitions containing List<string> m_strings = new List<string>() which don't look like they're going away any time soon :-( Feb 24, 2009 at 3:28
  • ^^ the point is, that duck typing works everywhere in ruby. You don't need interfaces, etc Feb 24, 2009 at 3:29
  • about the "mess of Assert.NotEqual<string> blah", that just depends on the library you are using Jafar solved that problem quite nicely themechanicalbride.blogspot.com/2009/06/… Jun 11, 2009 at 18:07
5

I am approaching it as an opportunity to highlight the benefits of Ruby over C#.

I'm not sure this is the right thing to do. If the tone of your talks is, "Ruby is cool because you can do x in it!" you'll lose your C# audience very quickly. They'll answer, "We can simulate x in C# if we want to, but we don't have much use for x in our designs." or perhaps, "If you think you need to do x then you're doing it wrong!"

They won't understand how Ruby can help them until they understand Ruby. Why not take them through some toy problems and show them how a Ruby programmer would solve them? Teach them the Ruby way. A week later, when they're looking at a problem they're having, one of them will say, "Well gee, I know how to solve this, but if I was using Ruby it would be a whole lot easier...."

4

Duck Typing! This will be less of an issue in c# 4.0, but there have been times when I had to duplicate whole blocks of code because two related classes with (for my purposes) identical APIs did not share a base class.

Also, blocks. C# has lambdas, but Ruby's syntax is prettier and they are used pervasively throughout the standard libraries. They are much more a part of idiomatic Ruby than idiomatic c#, and that counts for something.

Edit
Hash literals deserve a mention, too. In general I'd emphasize how concise you can be in Ruby, and how this allows you to express intent better and spend less time trying to make the compiler happy

0
1

Mix ins and multiple inheritance.

Its dangerous in the wrong hands, but quite beneficial for proper encapsulation of things, as opposed to having to inherit lots of things you don't necessarily need.

0
1

In addition to what everyone else have said, Open Classes is an important feature of Ruby that is worth mentioning: (example stolen from Ruby From Other Languages)

class Fixnum
  def hours
    self * 3600 # number of seconds in an hour
  end
  alias hour hours
end

# 14 hours from 00:00 January 1st
# (aka when you finally wake up ;)
Time.mktime(2006, 01, 01) + 14.hours # => Sun Jan 01 14:00:00

I know, patching like a monkey should be avoided, but I think that highlighting this feature to newcomers should give them an idea about the philosophy behind Ruby. Just remember to say: "Kids, don't try this at home!"

1
  • hmmm , somewhat equivalent to C#'s extensions methods(but may be different in implementation details)
    – niceman
    Jul 28, 2015 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.