不能在三元if语句中使用hash.key吗?

时间:2014-03-13 14:27:20

标签: ruby-on-rails hash ternary

我有一个包含键值对的哈希,我试图使用三元组,但它似乎没有用。

<%= @positions.key? 'dashboard' %> # => true

<%= @positions.key? 'dashboard' ? 'true text' : 'no text' %> # => false

我没有使用正确的代码吗?如果这不起作用,有没有办法检查是否存在使用三元if条件的散列键?

1 个答案:

答案 0 :(得分:3)

在参数:

周围添加括号
<%= @positions.key?('dashboard') ? 'true text' : 'no text' %>