String “true” and “false” to boolean

前端 未结 13 828
青春惊慌失措
青春惊慌失措 2020-12-08 18:23

I have a Rails application and I\'m using jQuery to query my search view in the background. There are fields q (search term), start_date, end

13条回答
  •  执笔经年
    2020-12-08 18:32

    Looking at the source code of Virtus, I'd maybe do something like this:

    def to_boolean(s)
      map = Hash[%w[true yes 1].product([true]) + %w[false no 0].product([false])]
      map[s.to_s.downcase]
    end
    

提交回复
热议问题