shuba.ivan shuba.ivan - 6 months ago 25
Ruby Question

Ruby count hash key

I have a hash like this:

{'yes' => 23,
'b' => 'travel',
'yesterday' => 34,
5 => '234',
:yesss => :fg,
try: 30,
key: 'some value',
'yesterday1' => 34,
'yesteryear' => 2014}


How can I count all keys which includes
yes
?

Answer

I suppose you meant:

your_hash.keys.count {|k| k.to_s.include?('yes')}
#=> 5