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

后端 未结 16 1263
夕颜
夕颜 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:18

    Have you tried Refinements?

    module Nothingness
      refine String do
        alias_method :nothing?, :empty?
      end
    
      refine NilClass do
        alias_method :nothing?, :nil?
      end
    end
    
    using Nothingness
    
    return my_string.nothing?
    

提交回复
热议问题