PHP's array_key_exists Corresponds to Ruby's Hash#key?

Tadashi Shigeoka ·  Sun, September 25, 2011

Processing like PHP’s array_key_exists is achieved in Ruby using Hash#key?.

■ PHP

$a = array('first' => 1, 'second' => 2);
var_dump( array_key_exists('first', $a) );
// => true

■ Ruby

animals = {:sheep => 1, :cow => 2};
puts animals.key?(:cow);
# => true

Besides include, there are methods like key, has_key, member, etc. to check if a key exists in a hash.

【Reference】

Convert PHP array_key_exists() to Ruby | PHP to Ruby

逆引きRuby - ハッシュ (Reverse Lookup Ruby - Hash)

That’s all from the Gemba.