Is There a Better Way of Checking Nil or Length == 0 of a String in Ruby?

后端 未结 16 1204
夕颜
夕颜 2020-12-04 07:57

Is there a better way than the following to check to see if a string is nil OR has a length of 0 in Ruby?

if !my_str         


        
相关标签:
16条回答
  • 2020-12-04 08:23

    For code golfers:

    if my_string=~/./
      p 'non-empty string'
    else
      p 'nil or empty string'
    end
    

    Or if you're not a golfer (requires ruby 2.3 or later):

    if my_string&.size&.positive?
      # nonzero? also works
      p 'non-empty string'
    else
      p 'nil or empty string'
    end
    
    0 讨论(0)
  • 2020-12-04 08:24

    If you are using rails, you can use #present?

    require 'rails'
    
    nil.present?  # ==> false (Works on nil)
    ''.present?    # ==> false (Works on strings)
    '  '.present?  # ==> false (Works on blank strings)
    [].present?    # ==> false(Works on arrays)
    false.present? # ==> false (Works on boolean)
    

    So, conversely to check for nil or zero length use !present?

    !(nil.present?)  # ==> true
    !(''.present?)    # ==> true
    !('  '.present?)  # ==> true
    !([].present?)    # ==> true
    !(false.present?) # ==> true
    
    0 讨论(0)
  • 2020-12-04 08:25

    An alternative to jcoby's proposal would be:

    class NilClass
      def nil_or_empty?
        true
      end
    end
    
    class String
      def nil_or_empty?
        empty?
      end
    end
    
    0 讨论(0)
  • 2020-12-04 08:28

    Konrad Rudolph has the right answer.

    If it really bugs you, monkey patch the String class or add it to a class/module of your choice. It's really not a good practice to monkey patch core objects unless you have a really compelling reason though.

    class String
      def self.nilorempty?(string)
        string.nil? || string.empty?
      end
    end
    

    Then you can do String.nilorempty? mystring

    0 讨论(0)
  • 2020-12-04 08:30

    As it was said here before Rails (ActiveSupport) have a handy blank? method and it is implemented like this:

    class Object
      def blank?
        respond_to?(:empty?) ? empty? : !self
      end
    end
    

    Pretty easy to add to any ruby-based project.

    The beauty of this solution is that it works auto-magicaly not only for Strings but also for Arrays and other types.

    0 讨论(0)
  • 2020-12-04 08:30

    First of all, beware of that method:

    As Jesse Ezel says:

    Brad Abrams

    "The method might seem convenient, but most of the time I have found that this situation arises from trying to cover up deeper bugs.

    Your code should stick to a particular protocol on the use of strings, and you should understand the use of the protocol in library code and in the code you are working with.

    The NullOrEmpty protocol is typically a quick fix (so the real problem is still somewhere else, and you got two protocols in use) or it is a lack of expertise in a particular protocol when implementing new code (and again, you should really know what your return values are)."

    And if you patch String class... be sure NilClass has not been patch either!

    class NilClass
        def empty?; true; end
    end
    
    0 讨论(0)
提交回复
热议问题