A .NET Guy Looking at Some Ruby (Part 2)

This is me continuing scribbling down notes as I go along relearning lost Ruby skills and at the same time comparing the Ruby syntax and way of writing code to .NET. In my previous post I very briefly went through the basics of variables, loops, iterators, conditionals and basic sorting. This post will look at classes, methods, inheritance and how to reuse Ruby code with include (require) statements.

Methods

Method or function declaration in Ruby is simple, perhaps even simpler than how VB does it:

def my_method(name)
    puts "Hello " + name.to_s
end

my_method "johan"
my_method("johan")

As you can see, you may call the method with or without parenthesis, to Ruby it doesn’t matter. I’ll have to read some more Ruby code to see if there are any exceptions or aesthetic rules to using parenthesis or not, but it seems that many Rubians use parenthesis when calling functions or methods on objects/classes, but leaving it when calling simple methods like puts and similar:

Oh, and Ruby doesn’t support method overloading! There are ways around this with default method parameters, add-ons to the language and so on, but basically – no method overloading. I’ll show some sample code later on when I get to class initialization.

Classes, Constructors and Properties

Ruby is of course object oriented, and very much so (without going into the “where everything is an object” speech). So, of course you can declare objects with properties and methods on them. Declaring a class is familiar:

class MyClass
end

You instantiate a class using a built in static “new” method on the class:

a_class = MyClass.new

Adding a (instance) method to the class is straight forward:

class MyClass
  def hello(name)
    puts name
  end
end

Static methods, or class methods, are declared by prefixing the method with “self.”, like this:

  def self.reverse_upcase(name)
    name.reverse.upcase
  end 

Adding a constructor or initializer to the class is done by declaring an initialize function:

  def initialize(name)
    @name = name
  end

As constructor overloading is not possible, you’ll have to add a default value to the parameter, making it optional in a VB-style of way:

  def initialize(name = "Anonymous")
    @name = name
  end

The @name variable is called an object variable and it is distinguished by the use of the ‘@’ sign. There is no need to declare the object variable in any other way, but to access the object variable (or property or attribute or…) you have to add property accessor code to the class:

  def name
    @name
  end

  def name=(name)
    @name = name
  end 

Now, class properties is used so much that there is a so called attribute accessor keyword available for you in Ruby, making things a bit simple to type:

attr_accessor :name

There are also other attribute keywords like attr_reader and attr_writer available for your pleasure.

Inheritance

Last thing to say about the basics around classes in Ruby is how to handle inheritance. The syntax is similar to c#, but instead of a colon (:), use the less than (<) operator. A very simple example of where the class Employee inherits from Person could look like this:

class Person
  attr_accessor :name

  def initialize(name)
    @name = name
    end
end

class Employee < Person
  attr_accessor :number

  def initialize(name, number)
    @name = name
    @number = number
  end

  def to_s
    @name + " (emp# " + @number + ")"
  end

end

e = Employee.new("Johan","123")

puts e
puts
e.is_a?(Person)
puts
e.is_a?(Employee)

Would print:

Johan (emp# 123)
true
true

Class Reuse

If you want to “use” or “include” Ruby code you’ve already written? Save your code (class) into a MyClass.rb file (as an example) and add a “require” statement to the top of your code where you want to reuse the MyClass class:

require "MyClass"

and classes in the MyClass.rb file will become available for you.

Wrapping Up

I guess that’s enough for now. I think this will be enough to be able to understand some Ruby code if you come across it, unless it contains too many weird class methods like Array#flatten and such J. Then there are a bunch of modules available to Ruby for doing a number of things. I may get into that in a later blog post, I’ll see.

All in all I think Ruby is cool. I’ve used dynamic languages and scripting languages before, but nothing as sleek as Ruby. Ruby is minimal code, fast to type and there’s a flow to the code which makes it quite easy to read.

Next

Next I’ll blog about downloading and building IronRuby and using it to call on .NET libraries, WPF and perhaps Silverlight (which seems to be all the buzz right now). I think I’ll look at exceptions and error handling after that and see if I can get some time to look at Rails again.

1 Comment

Comments have been disabled for this content.