为了澄清,这里about_regular_expressions.rb是我遇到问题的文件中的确切问题:
def test_sub_is_like_find_and_replace
assert_equal __, "one two-three".sub(/(t\w*)/) { $1[0, 1] }
end
Run Code Online (Sandbox Code Playgroud)
我知道答案是什么,但我不明白为了得到答案会发生什么.我对Ruby和regex都很陌生,特别是我对大括号之间的代码以及它是如何发挥作用感到困惑.