Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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 IRB是否调用特定的方法来生成检验输出?_Ruby_Irb - Fatal编程技术网

Ruby IRB是否调用特定的方法来生成检验输出?

Ruby IRB是否调用特定的方法来生成检验输出?,ruby,irb,Ruby,Irb,在irb中,regexp=/\A\d+->\d+\z/将返回值打印为/\A\d+->\d+\z/。对于不匹配的输入,我希望在错误消息中准确显示该字符串 但是,调用regexp.to_s或regexp.inspect都会产生稍微不同的字符串regexp.to_string不是一个方法 irb是否调用一个方法来获取它显示的值,如果是,是什么方法?regexp.inspect是您想要使用的方法。IRB转义反斜杠,但如果您实际输出字符串(例如,使用put),则这些附加反斜杠将不存在: >>

irb
中,
regexp=/\A\d+->\d+\z/
将返回值打印为
/\A\d+->\d+\z/
。对于不匹配的输入,我希望在错误消息中准确显示该字符串

但是,调用
regexp.to_s
regexp.inspect
都会产生稍微不同的字符串
regexp.to_string
不是一个方法


irb
是否调用一个方法来获取它显示的值,如果是,是什么方法?

regexp.inspect
是您想要使用的方法。IRB转义反斜杠,但如果您实际输出字符串(例如,使用
put
),则这些附加反斜杠将不存在:

>> regexp = /\A\d+->\d+\z/
/\A\d+->\d+\z/
>> regexp.inspect
"/\\A\\d+->\\d+\\z/"
>> puts _
/\A\d+->\d+\z/
nil

regexp.inspect
是您想要使用的。IRB转义反斜杠,但如果您实际输出字符串(例如,使用
put
),则这些附加反斜杠将不存在:

>> regexp = /\A\d+->\d+\z/
/\A\d+->\d+\z/
>> regexp.inspect
"/\\A\\d+->\\d+\\z/"
>> puts _
/\A\d+->\d+\z/
nil

inspect
由于转义字符的缘故,似乎产生了不同的输出。实际上,这是IRB用于输出的方法,很容易证明:

regexp.instance_eval { undef :inspect }
regexp
# => (Object doesn't support #inspect)

inspect
由于转义字符的缘故,似乎产生了不同的输出。实际上,这是IRB用于输出的方法,很容易证明:

regexp.instance_eval { undef :inspect }
regexp
# => (Object doesn't support #inspect)