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

    Posted on October 4th, 2009 RubyLove No comments

    The array_values() function in PHP takes an array and returns all it’s values as a numeric array.

    PHP

    $array = array('go' => 'green', 'stop' => 'red');
    var_dump( array_values($array) );
    /*
    Array (
    	[0] => green
    	[1] => red
    )
    */

    To replicate this functionality in Ruby, we need to use a Hash object, since arrays in Ruby don’t use associative key/value pairs.

    Ruby

    array = { :go => 'green', :stop => 'red' };
    puts array.values;
    # => ["green", "red"]

    Leave a reply