Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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 on rails 测试字符串是否没有内容_Ruby On Rails_Ruby_Regex_String - Fatal编程技术网

Ruby on rails 测试字符串是否没有内容

Ruby on rails 测试字符串是否没有内容,ruby-on-rails,ruby,regex,string,Ruby On Rails,Ruby,Regex,String,我想向测试无内容的ruby string对象发送一条消息: "".content? => false # same as .empty? " ".content? => false # same as .blank? "\n\t".content? => false # new feature 具体来说,content?如果有任何人可以在那里阅读的内容,则会传递为true。我将使用正则表达式对String类进行修补 "\n\t\n\n".gsub(/[\n|\t]/,''

我想向测试无内容的ruby string对象发送一条消息:

"".content? => false # same as .empty?
"   ".content? => false # same as .blank?
"\n\t".content? => false # new feature
具体来说,
content?
如果有任何人可以在那里阅读的内容,则会传递为true。我将使用正则表达式对String类进行修补

"\n\t\n\n".gsub(/[\n|\t]/,'').empty?

这让我很接近,但我可能正在重新发明控制盘,并希望有一个更完整的解决方案。

请在ActiveSupport中签出。

在检查之前将其剥离

“\n\t”.strip.empty?
像这样的吗

class String
  def content?
    self !~ /\A\s*\z/
  end
end

"".content? #=> false
"   ".content? #=> false
"\n\t".content? #=> false
"foo".content? #=> true

你就不能只做
~而不是将其包装在括号中并对整个表达式求反?当然可以,我只是从匹配的正则表达式开始,然后将求反放在它前面。现在修好了,谢谢!