Ruby: How to convert a string to boolean

前端 未结 14 833
抹茶落季
抹茶落季 2020-12-08 13:16

I have a value that will be one of four things: boolean true, boolean false, the string \"true\", or the string \"false\". I want to convert the string to a boolean if it i

14条回答
  •  清歌不尽
    2020-12-08 13:54

    h = { "true"=>true, true=>true, "false"=>false, false=>false }
    
    ["true", true, "false", false].map { |e| h[e] }
      #=> [true, true, false, false] 
    

提交回复
热议问题