Why do I love Ruby?
So mother (who can't program) just posed me the question
Why is Ruby your favourite programming language?
Me being a show off jumped straight into TextMate and banged out some code in real time to show her. First up, a quick little one-liner of Ruby code to output a String:
puts "Hello World"
# => "Hello World"
So she goes, "Sure, but whats so brilliant about that?" So I just decide to reverse the string, have it output in reverse order:
puts "Hello World".reverse
# => "dlroW olleH"
Then the next question comes, "So what makes that so much easier than in other languages?" Well now I was thinking on the spot about which other language I can bang out a quick example in without having to look up too much information. PHP seems the logical choice, being the language I know best behind Ruby.
Thinking about how to do it in PHP, I can't think of a function to reverse the content of a string, but I know that array_reverse() exists, so I just split it into an array and reverse that array. Only problem is I can't remember how to split a string by "", I don't think explode( "", $var ) does the job. So I quickly jump in and write the following code to test my concern.
<?php
$a = "Hello World"
$b = explode( "", $a );
$c = array_reverse( $b );
echo implode( "", $c );
?>
# => ERROR
The reason for the error is because I've missed a semi colon off the end of line 2, to this I get the response, "well thats certainly not as nice as ruby." Just because one little character is missing!
So I fix the semi colon and run it again, now I get an error complaining about explode not being able to split by a missing delimiter (the empty string - "") So I go hunting through the php.net docs and find str_split(), which does exactly what I want it to.
In replacing explode() with str_split() and running it via the php command line binary, I realise that I haven't got any \n (newlines) at the end of it, so it doesn't display nicely in the terminal. I thus update the script to the following and show her the result:
<?php
$a = "Hello World";
$b = explode( "", $a );
$c = array_reverse( $b );
echo implode( "", $c )."\n";
?>
# => "dlroW olleH"
And so she goes away seeing why I prefer Ruby to other languages for most programming I do. There are things Ruby fails at (and don't get me started on why rails isn't going to replace php!) and other places where it succeeds very well.
But each to their own, and my own favourite is Ruby!
Update
As pointed out in the comments, if I had looked a bit further I would've found strrev() which does the same as the reverse method in Ruby. So in fact the final code would be:
# Ruby Code
puts "Hello World".reverse
# PHP Code
echo strrev( "Hello World" );
So it turns out this was a bad way to show why I prefer Ruby to PHP code wise to mother, think I might have to just bite the bullet and write about why I prefer object.method to method( object )!
Comments
Well - that didn't work very well..
Arrr.. your comments are stipped of all things html or code looking..
Well - point is: use strrev()..
http://php.net/strrev
-fangel
There's always the strrev() function in PHP. But remembering the myriad of different function names (array_reverse, strrev, etc.) certainly isn't logical!
What if you missed the period before reverse or the p in puts? Same thing, one character and you get an error.
I don't do or know PHP, but I just found out it's this:
echo strrev("Hello world!");
Which is surprisingly "un-bad" for PHP :-D
I've not come across
strrev()before, and I guess googling for a function to do it might have been a quicker way to do it, but I was doing it off the top of my head.So this is A Bad Exampleâ„¢ to compare the languages then heh. I feel another blog post coming on about why I prefer
object.action.actiontoaction(action( object))differences in the languages.And fangel, comments are marked up using markdown, guess I need to tell people that, but yes I think
<code>tags are stripped out.@jimwhimpey
Thing is, you're more likely to miss the semi-colon out because its not part of the functional code, in that you're not misspelling a function or variable name, its a single character all on its own at the end of the line.
Reading the code through to double check it visually without running it, you're a helluva lot more likely to pick up on
uts "string"thanecho "string", both have 1 char missing, but one quickly reads like it will run and the other doesn't. Bit like you quickly readingcho "string";would figure quickly you've missed out a character.Plus if you're short on disk space it uses up 1B per line extra to a ruby script with the same character count (unlikely, and a totally moot point in this day and age!)
Regarding your object.action versus action(object) comment, you might be interested in Raganwald's recent post, http://weblog.raganwald.com/2008/01/no-detail-too-small.html
Leave a Reply