Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 [:punct:]在irb和rails测试中匹配不同_Ruby_Regex_Unit Testing_Ruby On Rails 4 - Fatal编程技术网

Ruby [:punct:]在irb和rails测试中匹配不同

Ruby [:punct:]在irb和rails测试中匹配不同,ruby,regex,unit-testing,ruby-on-rails-4,Ruby,Regex,Unit Testing,Ruby On Rails 4,当rails模型测试调用它时,它与任何标点符号都不匹配。使用以下代码 test "punctuation matched by [[:punct:]]" do punct_match = /\A[[:punct:]]+\Z/.match('[\]\[!"#$%&\'()*+,./:;<=>?@\^_`{|}~-]') puts punct_match puts punct_match.class end test“标点符号与[[:punct:][]匹配”do

当rails模型测试调用它时,它与任何标点符号都不匹配。使用以下代码

test "punctuation matched by [[:punct:]]" do
  punct_match = /\A[[:punct:]]+\Z/.match('[\]\[!"#$%&\'()*+,./:;<=>?@\^_`{|}~-]')

  puts punct_match
  puts punct_match.class
end
test“标点符号与[[:punct:][]匹配”do
点匹配=/\A[[:点:]+\Z/.match('[\]\[!“\$%&\'()*+,./:;?@\\{124;}-]))
把点刺放在火柴上
把点刺放在火柴上
结束
这将输出不可打印的字符和类

但是,如果我执行相同的语句

punct_match = /\A[[:punct:]]+\Z/.match('[\]\[!"#$%&\'()*+,./:;<=>?@\^_`{|}~-]')
punct\u match=/\A[[:punct:]+\Z/.match('[\]\[!“\$%&\'()*+,./:;?@\\\\{{124;}-]'))
在irb中正确匹配和输出

[\]\[!"#$%&'()*+,./:;<=>?@\^_`{|}~-]
=> nil
[\]\[!“\\$%&'()*+,./:;?@^^`{124;}~-]
=>零
我错过了什么

Ruby版本=>2.2.4,
Rails版本=>4.2.6

在ruby版本
2.4.0
中,
/[:punct:]/
的行为略有变化

是在ruby问题中提出的,它链接回Onigom中的问题,Onigom是ruby
2.0+
以来使用的regexp引擎


简而言之,在ruby版本中,
/[:punct:]/
与这些字符不匹配
/[:punct:]/
在ruby版本
2.4.0
中,
/[:punct:]/
的行为略有变化

是在ruby问题中提出的,它链接回Onigom中的问题,Onigom是ruby
2.0+
以来使用的regexp引擎


简单的回答是,在ruby版本中,这些字符没有与
/[:punct:]/
匹配
您在irb中到底输入了什么?输入:punct_match=/\A[[:punct:]]+\Z/.match(“[!”。/:;?^^^使用MRI v2.2.4?我试过了,但没有找到匹配的。无法复制。啊,等一下。您使用的是Ruby
2.4
而不是
2.2
,不是吗?这个正则表达式的行为确实发生了变化。您的rails和pry环境在不同的ruby版本中。哇!rails不是宝石吗?它应该使用与您所知道的ruby相同的ruby。您在irb中输入的具体内容是什么?输入:punct_match=/\A[[:punct:]+\Z/.match(“[[!”#$%&\'()*+,./:;?@^^
{124}-])输出:#使用MRI v2.2.4?我尝试了这个,但没有找到匹配项。无法复制。啊,等等。您使用的是Ruby
2.4
而不是
2.2
,不是吗?这个正则表达式的行为确实发生了变化。您的rails和pry环境在不同的Ruby版本中。哇!rails不是一个宝石吗?它应该使用与您知道的Ruby相同的Ruby。
$+<=>^`|~
/\A[[:punct:]]+\z/.match('[]!"#$%&\'()*+,./:;<=>?@^_`{|}~-]')