为什么 Array.to_s 返回括号?

发布于 2024-12-26 02:29:40 字数 526 浏览 0 评论 0原文

对于数组,当我键入:

puts array[0]

==> text 

然而当我键入时

puts array[0].to_s

==> ["text"]

为什么要使用括号和引号?我缺少什么?

附录:我的代码看起来像这样

page = open(url)  {|f| f.read }
page_array = page.scan(/regex/) #pulls partial urls into an array
partial_url = page_array[0].to_s
full_url = base_url + partial_url #adds each partial url to a consistent base_url
puts full_url

http://www.stackoverflow/["questions"]

For an array, when I type:

puts array[0]

==> text 

Yet when I type

puts array[0].to_s

==> ["text"]

Why the brackets and quotes? What am I missing?

ADDENDUM: my code looks like this

page = open(url)  {|f| f.read }
page_array = page.scan(/regex/) #pulls partial urls into an array
partial_url = page_array[0].to_s
full_url = base_url + partial_url #adds each partial url to a consistent base_url
puts full_url

what I'm getting looks like:

http://www.stackoverflow/["questions"]

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

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

发布评论

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

评论(4

灰色世界里的红玫瑰 2025-01-02 02:29:40

这将按原样打印数组,不带括号

array.join(", ")

This print the array as is without brackets

array.join(", ")
夜灵血窟げ 2025-01-02 02:29:40

to_s 只是检查 Array 类的别名。

这并不是说这意味着很多,除了期望 array.to_s 返回一个字符串之外,它实际上返回 array.inspect ,根据方法的名称,这并不是您真正想要的。

如果您只想要“胆量”,请尝试:

page_array.join

如果数组中有多个元素:

page_array.join(" ")

这将使得:

page_array = ["test","this","function"]

返回:

"test this function"

数组上的“to_s”返回什么,取决于您使用的 Ruby 版本,如上所述。 1.9.X 返回:

"[\"test\"]"

to_s is just an alias to inspect for the Array class.

Not that this means a lot other than instead of expecting array.to_s to return a string it's actually returning array.inspect which, based on the name of the method, isn't really what you are looking for.

If you want just the "guts" try:

page_array.join

If there are multiple elements to the array:

page_array.join(" ")

This will make:

page_array = ["test","this","function"]

return:

"test this function"

What "to_s" on an Array returns, depends on the version of Ruby you are using as mentioned above. 1.9.X returns:

"[\"test\"]"
往日情怀 2025-01-02 02:29:40

您需要向我们展示正则表达式才能真正正确修复此问题,但这可以做到:

将其替换

partial_url = page_array[0].to_s

为此

partial_url = page_array[0][0]

You need to show us the regex to really fix this properly, but this will do it:

Replace this

partial_url = page_array[0].to_s

with this

partial_url = page_array[0][0]
青巷忧颜 2025-01-02 02:29:40

这不一定能解决为什么您得到双倍数组的问题,但您可以将其展平,然后像这样调用第一个元素。

page_array = page.scan(/regex/).flatten

扁平化会取出堆叠数组并创建一个级别,因此如果您有 [1,2,[3,[4,5,6]]] 并对其调用 flatten,您将得到 [1,2,3,4,5,6]

它也比 array[0][0] 更健壮,因为,如果嵌套了两个以上的数组第一个元素,你会遇到同样的问题。

不过,伊恩是正确的,如果没有看到正则表达式,我们无法找出根本原因。

This doesn't necessarily fix why you are getting a doubled-up array, but you can flatten it and then call the first element like this.

page_array = page.scan(/regex/).flatten

Flattening takes out stacked arrays and creates one level, so if you had [1,2,[3,[4,5,6]]] and called flatten on it, you would get [1,2,3,4,5,6]

It is also more robust than doing array[0][0], because, if you had more than two arrays nested in the first element, you would run into the same issue.

Iain is correct though, without seeing the regex, we can't suss out the root cause.

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