什么是枚举器对象? (使用 String#gsub 创建)

发布于 2024-12-20 22:44:24 字数 553 浏览 0 评论 0原文

我有一个属性数组,如下所示,

attributes = ["test, 2011", "photo", "198.1 x 198.1 cm", "Photo: Manu PK Full Screen"]

当我这样做时,

artist = attributes[-1].gsub("Photo:")
p artist

我在终端中得到以下输出

#<Enumerator: "Photo: Manu PK Full Screen":gsub("Photo:")>

想知道为什么我会得到一个枚举器对象作为输出?提前致谢。

编辑: 请注意,我正在执行 attribute[-1].gsub("Photo:"),而不是 attributes[-1].gsub("Photo:", "") 所以想知道为什么枚举器对象返回这里(我期待一条错误消息)以及发生了什么?

红宝石 - 1.9.2

Rails - 3.0.7

I have an attributes array as follows,

attributes = ["test, 2011", "photo", "198.1 x 198.1 cm", "Photo: Manu PK Full Screen"]

When i do this,

artist = attributes[-1].gsub("Photo:")
p artist

i get the following output in terminal

#<Enumerator: "Photo: Manu PK Full Screen":gsub("Photo:")>

Wondering why am I getting an enumerator object as output? Thanks in advance.

EDIT:
Please note that instead of attributes[-1].gsub("Photo:", ""), I am doing attributes[-1].gsub("Photo:") So would like to know why enumerator object has returned here( I was expecting an error message) and what is going on.?

Ruby - 1.9.2

Rails - 3.0.7

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

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

发布评论

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

评论(2

白鸥掠海 2024-12-27 22:44:24

Enumerator 对象提供了一些方法常见的枚举 - nexteacheach_with_indexrewind 等。

您将得到 Enumerator 对象在这里因为 gsub 非常灵活:

gsub(pattern, replacement) → new_str
gsub(pattern, hash) → new_str
gsub(pattern) {|match| block } → new_str
gsub(pattern) → enumerator 

在前三种情况下,替换可以立即进行,并返回一个新字符串。但是,如果您不提供替换字符串、替换哈希或替换块,您将返回 Enumerator 对象,该对象可让您获取字符串的匹配部分以便稍后使用:

irb(main):022:0> s="one two three four one"
=> "one two three four one"
irb(main):023:0> enum = s.gsub("one")
=> #<Enumerable::Enumerator:0x7f39a4754ab0>
irb(main):024:0> enum.each_with_index {|e, i| puts "#{i}: #{e}"}
0: one
1: one
=> " two three four "
irb(main):025:0> 

An Enumerator object provides some methods common to enumerations -- next, each, each_with_index, rewind, etc.

You're getting the Enumerator object here because gsub is extremely flexible:

gsub(pattern, replacement) → new_str
gsub(pattern, hash) → new_str
gsub(pattern) {|match| block } → new_str
gsub(pattern) → enumerator 

In the first three cases, the substitution can take place immediately, and return a new string. But, if you don't give a replacement string, a replacement hash, or a block for replacements, you get back the Enumerator object that lets you get to the matched pieces of the string to work with later:

irb(main):022:0> s="one two three four one"
=> "one two three four one"
irb(main):023:0> enum = s.gsub("one")
=> #<Enumerable::Enumerator:0x7f39a4754ab0>
irb(main):024:0> enum.each_with_index {|e, i| puts "#{i}: #{e}"}
0: one
1: one
=> " two three four "
irb(main):025:0> 
行至春深 2024-12-27 22:44:24

当既没有提供块也没有提供第二个参数时,gsub 返回一个枚举器。请参阅此处了解更多信息。

要删除它,您需要第二个参数。

attributes[-1].gsub("Photo:", "")

或者

attributes[-1].delete("Photo:")

希望这有帮助!

When neither a block nor a second argument is supplied, gsub returns an enumerator. Look here for more info.

To remove it, you need a second parameter.

attributes[-1].gsub("Photo:", "")

Or

attributes[-1].delete("Photo:")

Hope this helps!!

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