One thing that has taken some getting used to as I learn C# is that it is a static language, whereas Ruby is a dynamic language. I am going to use this space to clean up some of my notes on the matter so I can reference them later, if I need to.
One of the main differences between the two is when using a dynamically typed language, the type of variable is checked at runtime, so in Ruby you can reassign a variable to a different type without running into an error. for example, below x is an integer, then a string, and there is no issue
x = 1
x = "string"
However, in C#, the variable is determined when it is compiled. The type of the variable has to be declared and it cannot be changed. The example above won’t work in C#. C# is a compiled language, which means that before it can run, the source code has to be translated by a compiler into executable machine code. After it is compiled, it can be run directly by the computer’s operating system. Ruby is more of an interpreted language, which means that it is read and executed line by line at runtime, without the step of compiling it first. There is more to it than that, but that is a quick summary.
int x = 1;
x = "string";
If you tried this, you are going to get a compile time error, which means that it cannot successfully translate the code into executable machine code.
Due to everything with compiling, there can be a performance difference between the two. Ruby is interpreted at runtime, and that can lead to slower performance. It does seem to be a more flexible language in general though. Also, the slower performance comment is based on my reading, I have no firsthand experience comparing the performance of the two.
On the other hand, because C# is compiled before execution, it will usually be faster in execution. Because of this, it catches errors when it is compiling, thus reducing runtime errors. This way you can catch mismatches early. Here is an example in each
def add(a, b)
a + b
end
add(5, "string")
The above, in Ruby, is obviously wrong because 5 and “string” cannot be added together. But, I’m not going to get an error until I run it. If I try to do the same thing in C#, I am going to get an error when it compiles, and I can fix it immediately.
public int Add(int a, int b)
{
return a + b;
}
Add(5, "string");
This is going to give me a compile-time error until I fix the mismatch that “string” is not an integer.
I think one of my main takeaways is that Ruby is great when you need to do something quickly…I am working on something with a friend and because the team writing the code consists of exactly one person, me, I am using Ruby on Rails. That being said, there are obviously plenty of huge applications and uses of Ruby, so it’s not limited to small quick applications.
I can see C# being very good for large complex systems where type safety can help prevent errors, and when you have a large team working together. There is also a lot that you can do in .NET that I haven’t had the chance to explore, but am looking forward to it as I keep working with it on le Bolongea Boulengarie. This is just speculation at the moment, based on what I have read and learned so far. Anyway, I think next time I am going to look at how C# handles arrays.
Leave a Reply