News for hackers. Stuff that matters.
Re:Ask HN: Why do people consider Ruby better than Perl or v... by rbrcurtis on Sunday January 09, 2011 @10:42PM (#2087362)
Can you explain the "makes perl look like java" comment? how does perl look like java?
Re:Ask HN: Why do people consider Ruby better than Perl or v... by sabat on Tuesday January 11, 2011 @03:13PM (#2093247)
I'm kind of kidding, but Ruby lets you do things more elegantly than you can in Perl, and with less verbosity (hence the Java comment). It's really a syntax issue that I'm talking about, but it matters a lot for readability and language usability.

Perl:

my $x = 'Apple Carrot Banana';

# sort them into an array

my @y = sort split(' ', $x);

# do we have more than 2 elements? (just a random test)

if (scalar(@y) > 2) {

  print "we've got ", scalar(@y), " elements!\n";
} else {

  print "insufficient elements. fail.\n";
}

Ruby:

x = 'Apple Carrot Banana'

y = x.split(' ').sort

if (y.size > 2)

  puts "we've got #{y.size} elements!"
else

  puts 'insufficient elements. fail.'
end

Ruby just seems more elegant to me, more programmer-friendly. YMMV.

Re:Ask HN: Why do people consider Ruby better than Perl or v... by chromatic on Tuesday January 11, 2011 @07:15PM (#2094243)
A modern Perl programmer might write something much more elegant:

    my $x = 'Apple Carrot Banana';
    my @y = $x->split(' ')->sort;

    if (@y > 2) {
        say "we've got ${\@y->size} elements!";
    } else {
        say "insufficient elements. fail.";
    }
Re:Ask HN: Why do people consider Ruby better than Perl or v... by perigrin on Tuesday January 11, 2011 @07:28PM (#2094283)
And one that doesn't like autobox would say:

    use 5.12.2; # enables modern features
    my $x = 'Apple Carrot Banana';
    my @y = sort split ' ', $x;

    say @y > 2 ? "we've got ${\scalar @y} elements!" 
               : 'insufficient elements. fail.';
Not everything needs to be an Object. Not every call needs to be a method call. Some people think better with procedures, some people think better with objects.

One of the reasons I personally like Perl is I can use which ever one is appropriate to the problem domain.

Edit: forgot the `sort` call.

Re:Ask HN: Why do people consider Ruby better than Perl or v... by colomon on Tuesday January 11, 2011 @09:04PM (#2094583)
Or ever-so-slightly lighter Perl 6 version:

    my $x = 'Apple Carrot Banana';
    my @y = $x.words.sort;

    if @y > 2 {
        say "we've got {@y.elems} elements!";
    } else {
        say "insufficient elements. fail.";
    }