Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • array_reverse

    Posted on October 25th, 2009 RubyLove No comments

    The array_reverse() function in PHP reverses the order of the elements in an array.

    PHP

    $a = array('php', 'ruby', 'java');
    $results = array_reverse($a);
    print_r($results);
    /*
    Array (
    	[0] => 'java'
    	[1] => 'ruby'
    	[2] => 'php'
    )
    */

    To replicate this functionality in Ruby, we can use the reverse method of the Array object.

    Ruby

    a = [ "php", "ruby", "java" ];
    p a.reverse;
    # => ["java", "ruby", "php"]

    Leave a reply