user1381745
user1381745

Reputation: 3900

Iterating over hash.keys and running through through case/when

{:category => 1}.keys.each do |n|
    case n
    when 'category'
        puts 'success'
    else
        puts "failure: #{n}"
    end
end

This ends with "failure: category", and I cannot for the life of me see why, so I'm reasonably sure that I'm doing something enormously stupid.

Upvotes: 0

Views: 41

Answers (2)

Marek Lipka
Marek Lipka

Reputation: 51191

You try to compare 'category' string and :category symbol - they are different:

'category' === :category
# => false

This should work:

{:category => 1}.keys.each do |n|
  case n
  when :category
    puts 'success'
  else
    puts "failure: #{n}"
  end
end

Upvotes: 2

Aguardientico
Aguardientico

Reputation: 7779

try changing when 'category' with when :category

Upvotes: 1

Related Questions