Ruby and Python

| 0 Comments

Before you can use Ruby on Rails you have to learn Ruby so I've spent part of the night trying to learn Ruby. What a fascinating language, and it's amazing how cool it is from just a syntactical perspective alone. Here's a simple example with an object in Ruby and a comparison with the equivalent in Python:

class PointKeeper

def initialize(name, count)

@name = name

@points = count

end

 

def Name

@name

end

 

def Points

@points

end

 

def Points=(points)

@points = points

end

end

 

pk = PointKeeper.new("MikeT", 4000)

 

print pk.Name + "t" + pk.Points.to_s + "n"

pk.Points = 5000

print pk.Name + "t" + pk.Points.to_s + "n"

 

<strong>Python version:</strong>

 

class PointKeeper:

def __init__(self, name, points):

self.name = name

self.points = points

 

pk = PointKeeper("MikeT", 4000)

print pk.name + "t" + str(pk.points) + "n"

pk.points = 5000

print pk.name + "t" + str(pk.points) + "n"

 

Now what's the big deal about the difference? Seems like the Python version is simpler and thus more elegant, right? Wrong. I have a soft spot for Python given that it was one of the first languages I studied, and I have even used it at work to get stuff done, but it is a bit lacking in some respects compared to Ruby and this happens to be one of them. Python doesn't allow you to restrict object member variables and that is a problem because it makes it harder to achieve proper encapsulation. Who knows what variables should be accessed and which shouldn't be accessed? That of course requires more documentation, which requires more reading, which ultimately means less time spent working on the actual code.

 

For a while I was resistant to learning Ruby, but now that Rails has come along, I have to learn it in order to feel like a proper geek. It's amazing how cool this language really is. I really do wish that I had been forced to get familiar with it a while ago.

Leave a comment

March 2010

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Recent Entries

A window into the totalitarian mind of the left on freedom of religion
From Digg: Me: I'm not going to hold my breath waiting for the same liberal democrats who shriek about the…
Google's lossy compiler
Google's closure compiler service gets a little too frisky under ADVANCED_OPTIMIZATIONS. Original code: With advanced optimizations enabled, it was able…
The three purposes of the federal income tax law
Businesses will spend about 3.4 billion man-hours and individuals about 1.7 billion hours figuring out their taxes this year.…

Subscribe

Advertisements

OpenID accepted here Learn more about OpenID