Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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 gsub方法的数组结果和匹配块_Ruby_Regex - Fatal编程技术网

使用ruby gsub方法的数组结果和匹配块

使用ruby gsub方法的数组结果和匹配块,ruby,regex,Ruby,Regex,我正在研究Ruby Koan,找到了一个让我很困惑的 "one two-three".gsub(/(t\w*)/) { $1[0, 1] } => "one t-t" 但是,当我修改$1变量的返回数组时,会得到一个令人困惑的结果 "one two-three".gsub(/(t\w*)/) { $1[1, 2] } => "one wo-hr" 给定第一个结果,我希望代码的第二位返回“one w-h”。为什么在第二个实例中返回两个字符 您期望“一个w-h”,这将是以下结果:

我正在研究Ruby Koan,找到了一个让我很困惑的

"one two-three".gsub(/(t\w*)/) { $1[0, 1] }
 => "one t-t"
但是,当我修改$1变量的返回数组时,会得到一个令人困惑的结果

"one two-three".gsub(/(t\w*)/) { $1[1, 2] }
 => "one wo-hr"
给定第一个结果,我希望代码的第二位返回“one w-h”。为什么在第二个实例中返回两个字符

您期望“一个w-h”,这将是以下结果:

"one two-three".gsub(/(t\w*)/) { $1[1, 1] }
[]
是一个可以提供如下范围的

str[start, length]
因此,代码中的2实际上是长度(即字符数)