gpt4 book ai didi

ruby - 灵活的引号是否会向字符串添加额外的字符?

转载 作者:数据小太阳 更新时间:2023-10-29 08:19:03 26 4
gpt4 key购买 nike

当我遇到这两个函数时,我正在通过 ruby​​ koans 学习 ruby​​:

def test_flexible_quotes_can_handle_multiple_lines
*long_string = %{
It was the best of times,
It was the worst of times.
}*
assert_equal *54*, long_string.size
end

def test_here_documents_can_also_handle_multiple_lines
*long_string = <<EOS
It was the best of times,
It was the worst of times.
EOS*
assert_equal *53*, long_string.size
end

问题是我无法理解使用灵活引号时这个额外字符的来源。 Ruby koans 说这两个答案都是正确的。

最佳答案

我会说它是 %{ 之后的换行符。

>> test = %{
">> foo
">> }
=> "\nfoo\n"
>> test.size
=> 5
>> test = %{foo
">> }
=> "foo\n"
>> test.size
=> 4
>> test = <<EOS
">> foo
">> EOS
=> "foo\n"
>> test.size
=> 4

关于ruby - 灵活的引号是否会向字符串添加额外的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8103791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com