为什么变量==“false”不能正常工作?

时间:2012-06-15 14:30:35

标签: ruby-on-rails haml

1)

- @client.getAvatars(@accountName).each do |avatar|
  =avatar['deleted']

输出: 假 假 真

2)

- @client.getAvatars(@accountName).each do |avatar|
 - if(avatar['deleted'] == 'false')
   =avatar['deleted']

空输出

3)

- @client.getAvatars(@accountName).each do |avatar|
 - if(avatar['deleted'] != 'false')
   =avatar['deleted']

输出: 假 假 真

我不明白为什么它如此奇怪,你能解释一下吗?

PS:我的工作变体是

- @client.getAvatars(@accountName).each do |avatar|
 - if(!avatar['deleted'])
   =avatar['deleted']

输出: 假 假

1 个答案:

答案 0 :(得分:6)

我认为问题是你认为avatar['deleted']返回一个字符串(单词“false”或单词“true”),当真正返回一个布尔值时。

试试这段代码:

- @client.getAvatars(@accountName).each do |avatar|
    - if(avatar['deleted'] == false) # or, if(avatar['deleted'])
        =avatar['deleted']

请注意false不是字符串,而是关键字:它是布尔标志。现在你应该得到的输出更符合你的第一个场景。