SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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:

    Code:
    <?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

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would depend on what your sever setup is. Are you using Rails? CGI? Something else?

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •