Different way of thinking PHP vs. Ruby
It’s interesting to note the differences between these two. I have found a few weeks before Peter Marklund’s excellent rails 101 pdf (a whopping 340+ pages of slides).
I am still going through it, but as I have a few years php experience (and still finding exciting new functions like compact and extract), I sometimes think about whether some of ruby’s features work the same way as php. The answer is usually: no.
For instance, in ruby you can do:
var1 = vara && varb && varc
a var is false if it’s ‘nil’ or ‘false’ everything else evaluates to true. It’s not the same with php, where 0 or ‘0′ also evaluates to false (and maybe some more).
In the above if all values eval’ to true then the last (varc) one’s value is put into var1. There’s also:
var1 = vara || varb || varc
Which works similarly, but the first variable that evaluates to true is assigned to var1.
My favorite one though, is how you can exchange the contents of two variables in ruby:
a, b = b, a
That’s all! (intuitive, you can’t deny)
It - of course - does not work with PHP. However since I was curious if a syntatically similar solution can be achieved with it - after some fiddling I come up with it for PHP:
list($a, $b) = array($b, $a);
Looks similarly (and as I do not know what happens behind the scenes in ruby they might just work the same way - but PHP exposes more of its inner workings)
Well, whatever. The more I know about ruby the more I like it. :)
About this entry
Title: Different way of thinking PHP vs. Ruby
- Published:
- 17:54, June 24, 2008
- Category:
- code, eng, php, ruby and rails
No comments
Jump to comment form | comments rss [?] | trackback uri [?]