Why Treat 0 as True in Ruby?

前端 未结 3 959
庸人自扰
庸人自扰 2020-12-30 19:45

I\'m reading through the excellent Ruby on Rails Tutorial and have encountered the following code.

if 0
  true
else
  false
end

The above r

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 20:07

    In Common Lisp, 0 is also treated as true. For example, the following code returns true.

    (if 0 'true 'false)
    

    No doubt, Ruby is following the same design decision made in Lisp. In Lisp, only an empty list (represented by nil) is false.

提交回复
热议问题