Author Topic: More Ruby  (Read 15091 times)

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
More Ruby
« on: June 23, 2005, 01:04:52 AM »
In the hopes of sparking a friendly debate,  I would like to start a thread that provides simple comparisons between the languages I've played with.  I'm also learning the Object Oriented scripting language Ruby, and  I don't really profess to know the other languages very well either.  :thumbsup:

I still like python the most.  Ruby is really concise.  There's acutally a lot going on in this single line of code, but i think it would be difficult to read for somone not familiar with the language.  The dangers that come from the "coolness" of conciseness are why i've never learned Perl.  It's hard not to write code like this if the language lets you.


certainly cool...but too Perl like?
ruby:
Code: [Select]

['apple', 'orange', 'pear', 'grape'].each { |fruit| puts fruit.length }


Simple...easy to read and concise....
python:
Code: [Select]

for fruit in ['apple', 'orange', 'pear', 'grape']:
    print len(fruit)


gets the job done in a reasonable amount of readable code.
php:
Code: [Select]

foreach (array('apple','orange','pear','grape') as $fruit) {
        echo strlen($fruit) . "\n";
}



Kidding me?  pretty verbose...but i think it's good to have a tool like Java or C# in the toolbox
java:
Code: [Select]

public class Test {
    public static void main (String[] args) {    
        String[] fruits = {"apple", "orange", "pear", "grape"};
        for (int i = 0; i < fruits.length: i++) {
            System.out.println(fruits[i].length());
        }
    }
}
« Last Edit: June 23, 2005, 01:10:38 AM by webwhy »

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
More Ruby
« Reply #1 on: June 23, 2005, 08:18:45 AM »
If going to Ruby vs. PHP is going to save me 10 keystrokes, I think I'll stick with PHP.  Besides, PHP appears to be more readable.  I'd rather have a language that is readable without having a ton of background knowledge than having a language that will save me an extra line of code.

Perspective

  • badfish
  • Jackass In Charge
  • Posts: 4635
  • Karma: +64/-22
    • http://jeff.bagu.org
More Ruby
« Reply #2 on: June 23, 2005, 11:36:39 AM »
If we're throwing python into the mix than ive got to side with that. Ive only been programming with python for a little while but its quickly becoming my favorite language.

7smurfs

  • Jackass III
  • Posts: 135
  • Karma: +12/-0
More Ruby
« Reply #3 on: June 23, 2005, 12:18:49 PM »
I'm sorry but that is an awful looking language.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
More Ruby
« Reply #4 on: June 23, 2005, 12:24:49 PM »
Readibility of code > ability to do it in fewer lines

Also if you had used the same formating for the Ruby as python and php it would be about the same length so there is no real gain on that statement

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
More Ruby
« Reply #5 on: June 23, 2005, 03:18:56 PM »
wow...nobody likes ruby...poor ruby:(

Quote
Also if you had used the same formating for the Ruby as python and php it would be about the same length so there is no real gain on that statement


True...
I did say that I still prefered the python approach over ruby to iterating over a collection.  It's just as concise and easier to read.  In my mind Python wins this comparison.

php resorts to the type specific strlen function and a string concatination.  I don't like that.  Is there any other way to find the length of a string in php? Not that i'm aware of...

I like how ruby has the length of the string as a method of a string object.
Code: [Select]
"myString".length  python's len() function takes a sequence object, which includes strings, as an argument.  Not as clean, but a least I only have on function to remember for lists, strings, and tuples.
Code: [Select]

myString = "test"
myList = ['list']
myTuple = ('tuple')
print len(myString), len(myList), len(myTuple)


The php approach takes a different function for each type, string or array.
Code: [Select]

$myString = "test";
$myArray = array("array");
echo strlen($myString) . "\n";
echo count($myArray) . "\n";


This is a trend that present throughout php.  There's also an annoying lack of inconsistency with how the functions are named.  I always have to check documentation for the correct name of the function I'm thinking of.  Is it is_array() or isarray()....isset() or is_set()
« Last Edit: June 23, 2005, 03:23:04 PM by webwhy »

Major_Small

  • Jackass IV
  • Posts: 317
  • Karma: +10/-0
    • http://www.johnshaoonline.com
More Ruby
« Reply #6 on: June 25, 2005, 02:29:35 AM »
Quote from: Mike
Readibility of code > ability to do it in fewer lines


most definately... consider:
Code: [Select]
#include

int main()
{
        for(short unsigned int R=0,G=0,B=0;(B<21);R=(R<5?R+1:R),G=(G<10?G+1:G),B=(B<21?B+1:B))
        {
                std::cout<<"R: "<        }
        return 0;
}
(don't forget efficiency of the algorithm is not what's in question here - and that was the best example I could think of ATM... don't really know where I got it from though.)

out of the ones listed, I'd go with PHP... don't forget about C/C++ as well  :D

and out of Java/C#, definately Java... but it's good to know not only as many languages as you can, but the science behind them... that's the key to being useful.
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
51 Members so far, are YOU a member?
Current team score: 827850 (ranked 357 of 41389)

The CBoard team is doing better than [COLOR='#0000FF']99.13%[/COLOR] of the other teams
Top 5 Members: Xterria(331048), Bennet(64957), pianorain(56621), Codeplug(35842), JaWiB(34917)

Last Updated on: Mon, 28 Nov, 2005 @ 3:21 PM EST