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

    Posted on February 24th, 2010 RubyLove No comments

    The array_keys() function in PHP takes an array as it’s argument and returns all the keys in that array (as a numeric array).

    PHP

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

    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.keys;
    # => [:go, :stop]

    Leave a reply