print

  • Printing output of a string of characters can be done in a variety of ways in Ruby. The equivalent of PHP’s print construct is Ruby’s print method.

    PHP

    print "Hello World";
    // => Hello World

    Ruby

    print "Hello World"
    # => Hello World

    Ruby also has the puts method, which will append a newline to the output.

    PHP

    print "Hello World\n";
    // => Hello World

    Ruby

    puts "Hello World"
    # => Hello World

    To inspect the value of a variable in Ruby we can use the p method. This is similar to performing a var_export in PHP.

    PHP

    $myVar = array("hello", "world");
    var_export($myVar);  
    // => array (0 => 'hello', 1 => 'world')

    Ruby

    my_var = ["Hello", "world"]
    p my_var
    # => ["Hello", "world"]

    see also

prev printf

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.