RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Ruby for C# Geeks : Page 4

As good as C# is, it's not always the best language for simple tasks. Enter Ruby, an interpreted, dynamically typed language that enables simple tasks with simple code.

The Ruby Mixin Feature
One trick I haven't covered is the ability of Ruby to change an existing class that has already been defined. This is called a mixin because it allows you to mix in your code with code that already exists. You can even create mixins that alter the built-in types in Ruby, effectively altering the way the entire language operates. To add even more variability, mixins allow you to import classes, methods, or extend classes with features of a Module (like a static class in C# 2.0) by modifying either the classes themselves or just particular instances of the classes!

You also can re-open an existing class definition and inject your own methods in, or override ones that are already there. For example, I could redefine the .to_s method of the Fixnum class, which is the class that all integers take on by default, to return something like "I, for one, welcome our robot overlords" every time. (The wisdom of doing something like this is of course questionable.) For the sake of demonstrating how very open the entire structure of Ruby is, here's how you can do this:

Fixnum.class_eval do
  def to_s
    "I, for one, welcome our robot overlords."

q = 123
puts q.to_s

Of course, don't try this at home, or at least don't do it in production code.

For Further Study
The Ruby language offers many more features and goodies than I could cover in one article, but absorbing them a few at a time from here on will be no harder than what this tutorial has shown. For a quick perusal of how Ruby generally compares to C# and Java, see Table 1 below.

Language Feature Ruby C# Java
Object Oriented Yes Yes Yes
Compiled/Interpreted Interpreted Compiled Both (usually compiled)
Variable Typing Dynamic Static Static
Native Mixins Yes No No (some add-on libraries offer limited support)
Closures Yes In version 2.0 + No
Reflection Yes Yes Yes
Multi-Threading Yes Yes Yes
Regular Expressions Native Standard library support Standard library support
Static and Instance Methods Yes Yes Yes
Type Inference Yes No No
Strong Typing Yes* Yes Yes
* Ruby is still strongly typed, but the type can be inferred and easily changed at run-time. Note that this is not the same as statically typed.
Table 1. Feature Comparison Chart of Ruby, C#, and Java

One of the particularly nice things about Ruby is the voluminous mass of documentation generated by the ever-expanding Ruby user community. If you would like to learn some more about Ruby, you should check out the industry standard tongue-in-cheek guide known as Why's (Poignant) Guide to Ruby. If humor isn't your ideal way to learn a language, then you might want to see the more formal Programming Ruby, which is actually quite authoritative on the subject. No matter what your preferences are, you can get wherever you want to go with Ruby from the official Ruby Language site.

Dave Dolan works as a Systems Administrator for Concurrent Technologies Corporation where he regularly builds .NET based Web applications to integrate and automate enterprise systems. He is currently studying the associative memory model of computing, and developing solutions utilizing associative database technology. For more of his writing, visit http://davedolan.com.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date