tags:

views:

32

answers:

1

I am trying to validate the values of certain keys in a hash:

response[:payment_status] == 'Completed' && response[:receiver_email] == '[email protected]' && response[:foo] == 'Bar'

While the above approach works I am quite sure there is a more elegant solution. I would hate to have a really long line if I add extra keys/values I want to validate.

P.S: I should mention I want a simple true/false returned.

+1  A: 

You could create a hash of the expect key/values and then map the input hash values:

expected = {'payment_status' => 'Completed', 'receiver_email' => '[email protected]' ... }
valid = expected.keys.all? {|key| response[key] == expected[key]}
Lee