Ruby vs C-Sharp or reason number 3259 I'm in love with Ruby
Oh Ruby! How I love thee! Let me count the ways… in lines of code. The fewer the lines… the more I love!
Ok, Ruby, I want so badly to create a series of new objects (to become database records). I’ve got 5 names which are the single property of the class “Role” which inherits Active Record. GO!
%w{god normal probation banned spam}.map{|n| Role.create(:name=>n)}
You’re done? You’ve created 5 new records in the database as well as 5 new objects that can be used right away? That’s, seriously, how much I love thee.
Ok, enough with the Elizabethan mockery. I seriously love it. I’m going to do a little compare and contrast of why, at night, I use Ruby over what I have to use, C#, during the day.
C# example of the same code with class.. Let’s assume there is not an equal to ActiveRecord in C#. There is; someone did an open source project that does it… pretty cool too, I’ll try and find and post the link to it for my poor .net brethren. oh, I’m also on a Mac without mono… so there may be typos in here, please post if I miss something.
//class pretend I've got my using statements.
public class Role
{
private string _Name;
public string Name
{
get{return _Name;}
set{_Name = value;}
}
public Role(){}
public void create()
{
//try catch omitted for space and time.
SqlConnection conn = new SqlConnection("myConnString");
SqlCommand com = new SqlCommand("INSERT INTO
tblRoles (name) VALUES (@name)", conn);
com.Parameters.Add(new SqlParameter("@name", this.Name));
conn.Open();
com.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
}
//implement it.
string[] names = new string[]{"god", "normal", "probation", "banned", "spam"}
foreach(string name in names)
{
Role r = new Role();
r.Name = name;
r.Create();
}
I count 31 lines of code. This examples uses standard C# code and techniques. Now, you know how this is gonna go… Here is the SAME thing in Ruby (specifically Ruby on Rails)
#class
class Role < ActiveRecord::Base
end
#implementation.
%w{god normal probation banned spam}.map{|n| Role.create(:name=>n)}
Ok, before any of you geeks out there call shenanigans on me let’s be a little honest. 31 lines of C# to 3 lines of Ruby may not be fair. I had to include the “create” method because I’m assuming no inheritence to “ActiveRecord” C#. I’m sure this would add a few lines to my Ruby class if I needed it.
The more compelling reason why I love Ruby is the implementation code: 1 line versus 5. This seems small but when you’re coding day in and day out, it can add up. Ruby keeps things simple, elegant and easy to implement. It’s a joy to use and frankly, I’m so glad that it came along when it did, cause man did I need a pick me up. For me, the move from Classic ASP to ASP.NET was a joyful one. ASP.NET 1.0/1.1 were breathtaking revelations when compared to Classic but with the bloat that is ASP.NET 2.0 and the marketing ploy that is ASP.NET 3.0, small compact and powerful languages like Ruby are beginning to prove their weight in gold.
Am I hating on C#? A little, but it could be worse. It could be Java (OOooo Snap! Oh no he didn!). Am I loving on Ruby? You bet your sweet ass I am. Loving every minute of it.
UPDATE! I’ve taken it back.
After reading the feedback, which has been very intelligent and well articulated, I’ve decided to write a follow up which can be read here. After thinking about what I said in this article, I realized one very important thing: That in the grand scheme of things… it doesn’t really matter. Code what you love and not what others say you should love. Please, check out the new article for continued insight to my feelings on this topic in light of some very compelling discussion from commentaries here as well as on dzone and reddit
articleStats
Here are some silly little facts about this Ruby vs C-Sharp or reason number 3259 I'm in love with Ruby...
article Links
These are the links that appear in this article. They probably don't make sense out of context... but just in case. :)
which can be read heredzone
man... these comparisons aren't very fair. i'm no fan of c#, but using the rails orm isn't a very fair comparison to using straight sql with c#.
don't forget to include your databases.yml in your line count as well.