Ruby -vs- PHP Showcase: nsupdate script
This a showcase of a Ruby script competing with a PHP script. The main focus is on useability and ease of implementation. The scope is a single simple script (say 10 to 100 lines).
(originally from Mar. 2007)
Please note that I don’t intend to benchmark the competitors. As I said the scope is limited and the focus on getting it done now. Then both could be run in client mode (off-web), CGI or fully embedded, I’m confident you can optimize if the need comes up. Also note this is an example to promote the use of Ruby not bash PHP.
The roots of PHP lie with PERL. And Ruby’s syntax is inspired by PERL. This way we should see some common elements in both PHP & Ruby and be able to do things with a similar approach.
nsupdate script in Ruby and PHP
The PHP nsupdate script copyright is with Chip Rosenthal, the minor formatting changes here are not intended for redistribution. The Ruby example was written alongside the PHP source and formatted to match the line numbers.
Some noteworthy differences:
-
The output templates are simple strings in PHP and inline here-documents in Ruby. There is no noticeable difference between those or the escapes to include variables. The ’eval’ statements at the end of the script are much simpler in Ruby though.
-
The Ruby script uses the postfix notation for conditionals which simplifies get_hostinfo() a bit. Also the hash merge eases applying default values here.
-
Ruby always returns a value, this is used in validate_host()
-
the Ruby script uses YAML to format the config file. YAML is very clean and effective here and comes readily integrated with Ruby. I recognize PHP scripts could use some bolt-on INI-style file reader to get par.
And here you go, the source code files for the Ruby -vs- PHP Showcase: nsupdate script. (I don’t use these scripts anymore. They should be fully useable still, YMMV.)
Source codes & Config files are attached below
I hope you get a feeling of Ruby’s way, see that the differences are not that vast, and perhaps you get eager to try this yourself!