我不明白为什么 string.size 返回它的作用

发布于 2024-11-15 09:57:27 字数 690 浏览 5 评论 0 原文

long_string = <<EOS
It was the best of times,
It was the worst of times.
EOS

返回 53。为什么?空白算不算?即便如此。我们如何得到53?

这个怎么样?

     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

是这种情况吗,因为 %{ case 将每个 /n 计为一个字符,并且认为第一行之前有一个,末尾有一个,然后在第二行末尾有一个,而在 EOS 的情况下,第一行之前只有一个,第一行之后只有一个?也就是说,为什么前者是54,后者是53呢?

long_string = <<EOS
It was the best of times,
It was the worst of times.
EOS

that returns 53. Why? The whitespace counts? Even still. how do we get 53?

How about this?

     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

Is this the case because the %{ case counts each /n as one character and theres considered to be one before the first line, one at the end, and then at the end of the 2nd line, whereas in the EOS case theres just one before the 1st line and one after the 1st line? In other words, why is the former 54 and the latter 53?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2024-11-22 09:57:27

对于:

long_string = <<EOS
It was the best of times,
It was the worst of times.
EOS

String is:
"It was the best of times,\nIt was the worst of times.\n"

It was the best of times, => 25
<newline> => 1
It was the worst of times. => 26
<newline> => 1
Total = 25 + 1 + 26 + 1 = 53

以及

long_string = %{
It was the best of times,
It was the worst of times.
}

String is:
"\nIt was the best of times,\nIt was the worst of times.\n"
#Note leading "\n"

如何工作:

< 的情况下,其后面的行是字符串的一部分。与 << 同一行的 << 之后以及到该行末尾的所有文本都将成为“标记”的一部分,用于确定何时字符串结束(在这种情况下,一行中的 EOS 本身与 < 匹配)。

对于 %{...},它只是用来代替 "..." 的不同分隔符。因此,当字符串在 %{ 之后开始新行时,该换行符就是字符串的一部分。

尝试这个示例,您将看到 %{...} 的工作方式与 "..." 相同:

a = "
It was the best of times,
It was the worst of times.
"
a.length # => 54

b = "It was the best of times,
It was the worst of times.
"
b.length # => 53

For:

long_string = <<EOS
It was the best of times,
It was the worst of times.
EOS

String is:
"It was the best of times,\nIt was the worst of times.\n"

It was the best of times, => 25
<newline> => 1
It was the worst of times. => 26
<newline> => 1
Total = 25 + 1 + 26 + 1 = 53

And

long_string = %{
It was the best of times,
It was the worst of times.
}

String is:
"\nIt was the best of times,\nIt was the worst of times.\n"
#Note leading "\n"

How it works:

In the case of <<EOS, the lines that follow it are part of the string. All the text after << on the same line as << and to the end of the line would be part of the "marker" that determines when the string ends (in this case an EOS on a line by itself is matching the <<EOS).

In case of %{...}, it is just a different delimiter used in place of "...". So when you have the string starting on a new line after a %{, that newline is part of the string.

Try this example and you will see how %{...} is working same as "...":

a = "
It was the best of times,
It was the worst of times.
"
a.length # => 54

b = "It was the best of times,
It was the worst of times.
"
b.length # => 53
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文