Generally which is better to use?:
case n
when 'foo'
result = 'bar'
when 'peanut butter'
result = 'jelly'
when 'stack'
result = 'overflow'
return result
or
map = {'foo' => 'bar', 'peanut butter' => 'jelly', 'stack' => 'overflow'}
return map[n]
More specifically, when should I use case-statements and when should I simply use a hash?
A hash is a data structure, and a case statement is a control structure.
You should use a hash when you are just retrieving some data (like in the example you provided). If there is additional logic that needs to be performed, you should write a case statement.
Also, if you need to perform some pattern matching, it makes sense to use a case statement: