Can anyone translate PHP to Ruby?

Ok so I’m a PHP programmer but I need to put a script on the site that uses Ruby. I know nothing about this language so I was hoping someone here could tell me what to do. Anyway in PHP I would do the following to get a variable from the url and display the correct page:


<?php
                
if(!isset($_GET['page'])){
                
        include('pages/home.htm');
                        
}else{
                
        include('pages/'.$_GET['page'].'.htm');
                        
}
                
?>

So now I need a Ruby script to do the same thing.

Thanks

It would depend on what your sever setup is. Are you using Rails? CGI? Something else?

I just wanted to add that there is no direct translation of your code to ruby for two reasons:

  1. In ruby, there are no tags like <?ruby ?>
  2. In ruby, there is no equivalent of the php include() statement.

php was designed for the web–while ruby is a general purpose programming language that has been applied to the web. ruby can certainly retrieve form data using cgi (and with a lot cleaner syntax than in php). And in ruby you can also create something called an erb file, which can contain arbitrary text, like html, with tags interspersed in the html that contain ruby code. But you have to explicitly execute the erb file to get ruby to replace the special tags with the output of the ruby code–the server does not do that automatically like with php.

7stud is correct. The two languages have fundamental differences in relation to the web. In PHP, the output of a script translates directly to the data sent back to the browser - this is rarely the case in Ruby as there’s advantage in sending STDOUT to the browser, although it’s certainly possible depending on your web server configuration. Additionally, Ruby doesn’t have opening and closing tags, a Ruby script is all ruby, there’s no cutting in and out of the interpreter. Ruby didn’t start as a web templating engine like PHP. You’d have to use a Ruby template engine like ERB to mix Ruby code with plain text (e.g. HTML, CSS).