Use an inline if or a ternary if operator:
shopping_list = {
"milk" => false,
"eggs" => false,
"jalapenos" => true
}
puts "Here is your Shopping List:"
shopping_list.each do |key, value|
puts "#{key} - #{if value then 'purchased' else 'not purchased' end}"
# or this:
# puts "#{key} - #{value ? 'purchased' : 'not purchased'}"
end
Prints:
Here is your Shopping List:
milk - not purchased
eggs - not purchased
jalapenos - purchased
Which operator to use: ternary operator (?:) or if/then/else/end?
I chose here if/then/else/end, but listed both options as acceptable. It is a matter of style which one you choose.
Some Stack Overflow Ruby users prefer to use a regular if ... then ... else ... end. It is longer, but more clear and more idiomatic. See, for example, these answers:
https://stackoverflow.com/a/2175392/967621
https://stackoverflow.com/a/4253250/967621
Other Stack Overflow users prefer ?:, which is more concise and clear, if you are used to it. Also note that The Ruby Style Guide agrees:
Prefer the ternary operator(?:) over if/then/else/end constructs. It’s more common and obviously more concise.
# bad
result = if some_condition then something else something_else end
# good
result = some_condition ? something : something_else