Ruby on Rails for a .NET Developer – Part 2

Ok, so I thought the whole scaffolding thing was so easy and I would be done in a few minutes. I was so wrong. Nothing seemed to work right. I would call ruby script/generate scaffold Tag and it will generate all the files but the views were empty. Hmm… This is exactly what has been done in all the demos and tutorials. Apparently, things have changed in Rails 2.0 and I had to call this instead script/generate scaffold Tag tag:string.

One thing I don’t like so far is the lack of good documentation for Rails 2.0 and there aren’t any books out there. I went ahead and bought me of Agile Web Development with Rails which has been updated for 2.0 but you can only get as beta from The Pragmatic Bookshelf. I bought the PDF version and have been reading it for the past few days.

It occurred to me that before I can get too deep into Rails, I need to learn a little about Ruby. After all the syntax is very different from C# or vb.net. Everyone claims that it is an easier language to understand but I think that is very subjective. Spanish might be easier than Chinese but if you are Chinese you might not think so.

With that said, in this post I am going to list a few things I have learned over the past few days about Ruby Syntax and how it compares to C# syntax.

Comments

C#

//this is a one line comment

/* This is a multi-line
comment
*/

Ruby

# this is a one line comment

=begin
this is a mult-line
comment
=end

Winner: C#

Class Definition

C#

class Car : Vehicle
{
public Car()
{
}
}

Ruby

class Car < Vehicle
def initialize
end
end

Winner: tie but if you hate curly brackets then Ruby wins

Method Definition

C#

public int GetYear()
{
}

public static int GetYear(string carName)
{
}

Ruby

def GetYear
end

def self.GetYear(carName)
end

Note: You can use the class name instead of self to make the method static. Also, in Ruby they are called class methods and not static methods. Come to think of it, that makes sense, since the method is defined for the class and not the instance or the object.

Winner: Ruby

Variables

Local variables

C#

int aNumber = 4; //declared inside a method

Ruby

aNumber=4 # no declaration needed just use it inside a method

Instance variables (fields)

C#

int aNumber; //declared inside a class not a method

Ruby

@aNumber #no declaration needed just use it anywhere

Static variables (fields)

C#

static int aNumber; //declared inside a class not a method

Ruby

@@aNumber #no declaration needed just it anywhere

Control Structure

if

C#

if (aCondition)
{
}
else
{
}

Ruby

if aCondition then
else
end

unless

C#

There is no unless in C# but the same logic can be written like this:

if (!aCondition)
{
}
else
{
}

Ruby

unless isBlue

else

end

case

C#

switch (year)
{
case 1960:
case 1961:
case 1962:

case 1969:
break;
case 1970:

case 1979:
break;
default:
}

The code above has been truncated for brevity because you cannot do ranges in C# as easy you can in Ruby

Ruby

case year
when 1960 .. 1969
when 1970 .. 1979
else
end

The above Ruby code is complete and not truncated. The express 1960 .. 1969 specifies a range.

while

C#

while (aCondition)
{
}

Ruby

while aCondition
end

until

C#

There is no until in C# but the same logic can be expressed like this:

while (!aCondition)
{
}

Ruby

until aCondition
end

This is not a comparison of language features, it is just a comparison of the syntactical differences of *some* of both languages. This is *not* an exhaustive list and is not meant to be – a simple Google search will get you thousands of pages of syntax goodies for either language.

So far, I can’t say I love Ruby’s syntax but if I were new to programming, I would say it looks more readable and easier to understand. Being a C#/VB developer for so long, I am a little confused and uncomfortable with the Ruby syntax. But, the facts that I have used VB for the past 10 or so years and that I hate semi-colons and curly brackets in C#, it is not that bad.

Advertisements

0 thoughts on “Ruby on Rails for a .NET Developer – Part 2

  1. Hi Emad,nice entry. 🙂 A couple of considerations for you:1) In the Class definition snippet, you probably meant class Car not Product;2) In order to make the snippets more Rubyesque, you may want to consider switching from camelCase to snake_case.By the way, I’m in the process of writing a Rails book for .NET and Microsoft developers, that will be published before Christmas. By then you’ll be a Rails pro, but I wanted to give you the heads up. :-)Cheers,Antonio

    Like

  2. Hi Emad,

    nice entry. 🙂 A couple of considerations for you:

    1) In the Class definition snippet, you probably meant class Car not Product;
    2) In order to make the snippets more Rubyesque, you may want to consider switching from camelCase to snake_case.

    By the way, I’m in the process of writing a Rails book for .NET and Microsoft developers, that will be published before Christmas. By then you’ll be a Rails pro, but I wanted to give you the heads up. 🙂

    Cheers,
    Antonio

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s