Ken Muse

Ruby for the .NET Developer


The last few weeks, I’ve been having to work in Ruby 2.x. It’s not a language that I’ve spent a great deal of time with, so it’s been fun to dive into the nuances of the platform. There’s a lot of things to like about it, including some simple syntaxes for common tasks in other languages. It can make it a powerful tool for some complex jobs. As someone that programs in multiple different languages, I often see many aspects that are similar, as well as certain aspects that make the language more powerful by requiring less code.

For the benefit of others that might be trying to learn either language, I’ll show a few pieces of code in both Ruby and C# (which is similar to Java and JavaScript). There’s obviously lots of ways to do the same tasks, but hopefully this helps you to at least draw some parallels.

RubyC#Comment
1%w[this is some text]
1["this", "is", "some", "text"]
Ruby has a simple syntax for string arrays.
1def doSomething(text:, count:)
1void DoSomething(string text, int count)
Ruby allows named parameters and they can be provided in any order if they are named (end with :). C# expects a specific order and type. Ruby allows explicit return values (like C#), but also supports implicit (returning the value from the last evaluated statement).
1lambda { |name| "Hello #{name}" }
1name => $"Hello {name}"
Lambda expressions and string interpolation can be very similar in both languages
1lookup =
2{
3  "key1": "value1",
4  "nested": {
5    "key2": "value2"
6  }
7}
1var lookup = = new Dictionary<string, object>()
2{
3  { "key1", "value1"},
4  { "nested", new Dictionary<string, object>()
5    {
6      { "key2", "value2" }
7    }
8  }
9};
Configuring a Dictionary (C#) or Hash (Ruby) isn’t tough, but it’s definitely more elegant in Ruby!
1 isConfigured ? "yes" : "no" 
1 isConfigured ? "yes" : "no" 
Ternary operators. It’s nice when they just work. ❤️
1items.each do |item|
2  total += item.value
3end
1foreach (var item in items)
2{
3    total += item.value;
4}
A simple for-each loop
1myArr =  items.map do |item|
2  item.value
3end
1var myArr = items.Select(item => item.value);
Iterate each item in items, returning the .value property and creating an iterable entity.
1myArr = [1, 2, 3, 4]
2myArr << 5
 1// .NET prefers collections
 2var myArr = List<string>([1, 2, 3, 4]);
 3myArr.Add(5);
 4
 5// But arrays + LINQ is possible
 6var myArr = new[] {1, 2, 3, 4};
 7myArr = myArr.Append(5).ToArray();
 8
 9// And you can use pure arrays
10var myArr = new[] {1, 2, 3, 4};
11Array.Resize(ref myArr, myArr.Length+1);
12myArr[myArr.Length - 1] = 5;
With C#, you want to use a List<T> to work with mutable lists of values. With .NET, you tend to prefer to work with enumerable collections as opposed to using ToArray to return to an array. To work with an Array natively, you need to resize the array in order to add a value. Ruby hides the technical aspects of reallocating arrays. C# can do this as well … if you stick to using Collections.
1element = items.detect { |item| item[0] == "value" }
1var element = items.FirstOrDefault(item => item[0] == "value")
Finding the first member of a collection that matches a criteria is very similar, esp. if you’re using C#’s Language-Integrated Query ( LINQ )