String “true” and “false” to boolean

前端 未结 13 764
青春惊慌失措
青春惊慌失措 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:39

    I don't think anything like that is built-in in Ruby. You can reopen String class and add to_bool method there:

    class String
        def to_bool
            return true if self=="true"
            return false if self=="false"
            return nil
        end
    end
    

    Then you can use it anywhere in your project, like this: params[:internal].to_bool

    0 讨论(0)
  • 2020-12-08 18:39

    You could consider only appending internal to your url if it is true, then if the checkbox isn't checked and you don't append it params[:internal] would be nil, which evaluates to false in Ruby.

    I'm not that familiar with the specific jQuery you're using, but is there a cleaner way to call what you want than manually building a URL string? Have you had a look at $get and $ajax?

    0 讨论(0)
  • 2020-12-08 18:46

    As far as i know there is no built in way of casting strings to booleans, but if your strings only consist of 'true' and 'false' you could shorten your method to the following:

    def to_boolean(str)
      str == 'true'
    end
    
    0 讨论(0)
  • 2020-12-08 18:47

    ActiveRecord::Type::Boolean.new.type_cast_from_user does this according to Rails' internal mappings ConnectionAdapters::Column::TRUE_VALUES and ConnectionAdapters::Column::FALSE_VALUES:

    [3] pry(main)> ActiveRecord::Type::Boolean.new.type_cast_from_user("true")
    => true
    [4] pry(main)> ActiveRecord::Type::Boolean.new.type_cast_from_user("false")
    => false
    [5] pry(main)> ActiveRecord::Type::Boolean.new.type_cast_from_user("T")
    => true
    [6] pry(main)> ActiveRecord::Type::Boolean.new.type_cast_from_user("F")
    => false
    [7] pry(main)> ActiveRecord::Type::Boolean.new.type_cast_from_user("yes")
    DEPRECATION WARNING: You attempted to assign a value which is not explicitly `true` or `false` ("yes") to a boolean column. Currently this value casts to `false`. This will change to match Ruby's semantics, and will cast to `true` in Rails 5. If you would like to maintain the current behavior, you should explicitly handle the values you would like cast to `false`. (called from <main> at (pry):7)
    => false
    [8] pry(main)> ActiveRecord::Type::Boolean.new.type_cast_from_user("no")
    DEPRECATION WARNING: You attempted to assign a value which is not explicitly `true` or `false` ("no") to a boolean column. Currently this value casts to `false`. This will change to match Ruby's semantics, and will cast to `true` in Rails 5. If you would like to maintain the current behavior, you should explicitly handle the values you would like cast to `false`. (called from <main> at (pry):8)
    => false
    

    So you could make your own to_b (or to_bool or to_boolean) method in an initializer like this:

    class String
      def to_b
        ActiveRecord::Type::Boolean.new.type_cast_from_user(self)
      end
    end
    
    0 讨论(0)
  • 2020-12-08 18:49

    You can use wannabe_bool gem. https://github.com/prodis/wannabe_bool

    This gem implements a #to_b method for String, Integer, Symbol and NilClass classes.

    params[:internal].to_b
    
    0 讨论(0)
  • 2020-12-08 18:52

    There isn't any built-in way to handle this (although actionpack might have a helper for that). I would advise something like this

    def to_boolean(s)
      s and !!s.match(/^(true|t|yes|y|1)$/i)
    end
    
    # or (as Pavling pointed out)
    
    def to_boolean(s)
      !!(s =~ /^(true|t|yes|y|1)$/i)
    end
    

    What works as well is to use 0 and non-0 instead of false/true literals:

    def to_boolean(s)
      !s.to_i.zero?
    end
    
    0 讨论(0)
提交回复
热议问题