Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有更好的方法来检查Ruby中字符串的Nil或Length==0?_Ruby - Fatal编程技术网

有没有更好的方法来检查Ruby中字符串的Nil或Length==0?

有没有更好的方法来检查Ruby中字符串的Nil或Length==0?,ruby,Ruby,有没有比下面更好的方法来检查Ruby中的字符串是nil还是长度为0 if!my_string | | my_string.length==0 返回真值 其他的 返回错误 结束 在C#中有方便的方法 string.IsNullOrEmpty(myString) 与Ruby中类似的任何东西?每个类都有一个nil?方法: if a_variable.nil? # the variable has a nil value end if a_string.empty? # the s

有没有比下面更好的方法来检查Ruby中的字符串是nil还是长度为0

if!my_string | | my_string.length==0
返回真值
其他的
返回错误
结束
在C#中有方便的方法

string.IsNullOrEmpty(myString)

与Ruby中类似的任何东西?

每个类都有一个
nil?
方法:

if a_variable.nil?
    # the variable has a nil value
end
if a_string.empty?
    # the string is empty
}
并且字符串具有
空?
方法:

if a_variable.nil?
    # the variable has a nil value
end
if a_string.empty?
    # the string is empty
}

请记住,字符串为空时不等于
nil
,因此请使用
empty?
方法检查字符串是否为空。

nil?
在布尔上下文中可以省略。通常,您可以使用它来复制C代码:


康拉德·鲁道夫的答案是正确的

如果它真的让您感到不舒服,可以使用monkey修补String类或将其添加到您选择的类/模块中。除非你有一个非常令人信服的理由,否则对核心对象进行修补并不是一个好的做法

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

然后你可以做
String.nilorempty?mystring

jcoby提案的替代方案是:

class NilClass
  def nil_or_empty?
    true
  end
end

class String
  def nil_or_empty?
    empty?
  end
end

首先,要注意这种方法:

正如上面所说:

“这种方法似乎很方便,但大多数时候我发现这种情况是因为试图掩盖更深层次的错误

您的代码应该遵守关于字符串使用的特定协议,并且您应该了解在库代码和正在使用的代码中使用该协议

NullOrEmpty协议通常是一个快速修复(因此真正的问题仍然存在于其他地方,并且您使用了两个协议),或者是在实现新代码时缺乏特定协议的专业知识(同样,您应该真正知道返回值是什么)。”

如果你修补字符串类。。。一定要


如果您愿意需要ActiveSupport,您可以使用
#blank?
方法,该方法是为NilClass和String定义的。

正如在Rails(ActiveSupport)之前所说,它有一个方便的方法,实现方式如下:

class Object
  def blank?
    respond_to?(:empty?) ? empty? : !self
  end
end
很容易添加到任何基于ruby的项目中


此解决方案的优点在于它不仅适用于字符串,而且适用于数组和其他类型。

当我不担心性能时,我通常会使用以下方法:

if my_string.to_s == ''
  # It's nil or empty
end
当然,有各种各样的变化

if my_string.to_s.strip.length == 0
  # It's nil, empty, or just whitespace
end

变量。空白?我会的。
如果字符串为空或字符串为零,则返回true。

我喜欢按如下方式执行此操作(在非Rails/ActiveSupport环境中):

这是因为:

nil.to_s == ""
"".to_s == ""
在普通Ruby中检查空字符串,同时避免NameError异常 这里有一些很好的答案,但是您不需要ActiveSupport或monkey补丁来解决这里的常见用例。例如:

my_string.to_s.empty? if defined? my_string
如果my_字符串为nil或空字符串,这将“做正确的事情”,但如果my_字符串未定义,则不会引发错误。这通常比更人为的更可取:

my_string.to_s.empty? rescue NameError
或者更为冗长,因为对于不希望发生的事情,应该保存异常。在这种情况下,虽然这可能是一个常见错误,但未定义的变量并不是真正的例外情况,因此应该相应地处理它


您的里程数可能会有所不同。

另一个选项是动态将nil转换为空结果:

(我的字符串)。空?

对于代码高尔夫球手:

如果我的字符串=~//
p'非空字符串'
其他的
p'零或空字符串'
结束
或者,如果您不是高尔夫球手(需要ruby 2.3或更高版本):

如果我的字符串大小为正?
#非零?也有效
p'非空字符串'
其他的
p'零或空字符串'
结束

在rails中,您可以尝试
#blank?

警告:当字符串由空格组成时,它将为您提供正数:

nil.blank? # ==> true
''.blank? # ==> true
'  '.blank? # ==> true
'false'.blank? # ==> false
我只是想指出这一点。也许它适合你的需要

UPD。为什么我的feed中会出现老问题?
很抱歉发帖。

您尝试过改进吗

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?

如果您使用的是rails,则可以使用
#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)
!(nil.present?)  # ==> true
!(''.present?)    # ==> true
!('  '.present?)  # ==> true
!([].present?)    # ==> true
!(false.present?) # ==> true
因此,反过来,要检查长度为零或零,请使用
!是否存在?

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)
!(nil.present?)  # ==> true
!(''.present?)    # ==> true
!('  '.present?)  # ==> true
!([].present?)    # ==> true
!(false.present?) # ==> true

请注意,仅包含空格的字符串仍被视为空白。那么是空白?为true。此外,布尔值false、{}和[]被视为空。即{}.blank?,[].blank?,false.blank?所有计算结果都为true。像这样的if语句是多余的:“if(condition)then true else false”。条件本身与if语句具有相同的真实性。
my_string | | my_string.length==0
将在
my_string
为零时引发
NoMethodError
。我想你的意思是:
my_string.nil?|my_string.length==0
应该是
!my_string | | my_string.length==0
但是。。但是ruby核心库已经实现了这一点。nil.to_s=>“”,所以你可以说thing.to_s==“”@DigitalRoss这避免了字符串转换,这可能会或可能不会是一个轻微的性能问题。实际上,这是一个只支持rails的扩展,而不是纯ruby。但是如果您使用的是rails,它非常方便!使用
strip
的变化可能效率很低。另一个有趣的方法是my_string.to_s.empty?对于大多数人来说,第一个选项是完全可以接受的,而且读起来也很好。一个很好的解释为什么
string.to_s.empty?
nil
都有效。