Why Ruby rocks
Just for honks & giggles, I've been messing around with rewriting/refactoring some of the code samples I've done over the years in my favorite languages to compare syntax verbosity. Ruby totally kicks ass. Consider the following method to print image filenames on disk, using most best practice conventions for indenting, bracketing, etc.:
Ruby:
def get_all_photos
Photo.find(:all).each { |p| puts p.filename }
end
Python:
def get_all_photos():
photos = Photo.find_all
for p in photos:
print p.filename
JavaScript:
function getAllPhotos() {
var photos = Photo.find();
for(var i=0;i<photos.length;i++) {
document.write(p.filename);
}
}
C#/Java:
private static void GetAllPhotos()
{
string p = Photos.find();
for(int i=0;i<p.Length;i++)
{
Console.WriteLine(p.Filename);
}
}
Ruby:
def get_all_photos
Photo.find(:all).each { |p| puts p.filename }
end
Python:
def get_all_photos():
photos = Photo.find_all
for p in photos:
print p.filename
JavaScript:
function getAllPhotos() {
var photos = Photo.find();
for(var i=0;i<photos.length;i++) {
document.write(p.filename);
}
}
C#/Java:
private static void GetAllPhotos()
{
string p = Photos.find();
for(int i=0;i<p.Length;i++)
{
Console.WriteLine(p.Filename);
}
}
2 Comments:
At December 13, 2006 1:29 AM,
Jeff Lewis said…
Actually for C# you can get pretty close:
static void GetAllPhotos()
{
Photo.FindAll().ForEach(delegate(Photo p) { Console.WriteLine(p.Filename); });
}
At April 14, 2008 12:54 PM,
Leah said…
Actually, shorter Python would be:
def get_all_photos():
print [p.filename for p in Photo.find_all()]
Personally, I don't know why this should be called get_all_photos rather than print_photo_names... or why you would rather write short unreadable Ruby code. Like, wtf is 'lpl'?
Post a Comment
Links to this post:
Create a Link
<< Home