Why I love ruby: arrays
I work on a number of projects, some in ruby, some in “other” languages. Time and time again I find myself working on a piece of code in those “other” projects and thinking “man I wish this was in ruby”. The most recent one was with arrays in PHP, so I figured I’d write a little piece on why it made me sad. Sometimes, the simple things in a language are what make it pleasing to work in.
While not strictly necessary to do an is_array() check, not doing so has bitten me on numerous occasions when $emotions is something other than an array for some reason.
$emotions = array('happy', 'sad', 'angry');
if (is_array($emotions))
foreach($emotions as $e) {
print $e . "\n";
}Alternately, you can typecast the array and not need to check is_array() - this is my usual methodology. But typecasting just feels like C..yech!
$emotions = array('happy', 'sad', 'angry');
foreach((array)$emotions as $e) {
print $e . "\n";
}or even for full key/value pair
$emotions = array('happy', 'sad', 'angry');
foreach((array)$emotions as $k => $v) {
print "[$k] => $v\n";
}And lastly you can loop through it. I won’t get into the nitty gritty details as to why you might wish to do this, if you grok php, you already know why.
$emotions = array('happy', 'sad', 'angry');
for ($i = 0; isset($emotions[$i]); $i++) {
print $emotions[$i] . "\n";
}Compare the above to this little ruby snippet
emotions = ['happy', 'sad', 'angry']
emotions.each {|e| puts e}or even this somewhat unfair comparison
['happy', 'sad', 'angry'].each {|e| puts e}and lastly…key/value
emotions = ['happy', 'sad', 'angry']
emotions.each_index {|k| puts "[#{k}] => #{emotions[k]}"}of course key/value in an array in ruby isn’t necessarily as meaningful as PHP where arrays can be overloaded as hashes.
OK…enough of the language semantics. PHP is fine, I’m not posting to bitch about the language, but more to praise how simple ruby can be in dealing with common array looping.
For those wondering where the pedantic comparison is for my first example… :)
emotions = ['happy', 'sad', 'angry']
emotions.each {|e| puts e} if emotions.is_a? Arrayalthough I’ve yet to run into an instance in ruby where I’ve burned myself like I have in PHP. Although I suspect that is more circumstance related than anything to do with the language itself.
Update I forgot to mention that I also totally love flatten!
happy = ['ecstatic', 'joyous']
sad = ['depressed', 'suicidal']
angry = ['raging', 'mad']
emotions = [happy, sad, angry]
emotions.flatten.each {|emotion| puts emotion}really handy for when you need to merge a few arrays, or the result of using collect. While not horrible in PHP, it’s not the worst thing ever (just more typing!)
$happy = array('ecstatic', 'joyous');
$sad = array('depressed', 'suicidal');
$angry = array('raging', 'mad');
$emotions = array_merge_recursive($happy, $sad, $angry);
foreach((array)$emotions as $k => $v) {
print "[$k] => $v\n";
}Posted in Ruby | no comments | atom
Trackbacks
Use the following link to trackback from your own site:
http://www.rubyfindings.com/trackbacks?article_id=why-i-love-ruby-arrays&day=16&month=06&year=2008