gpt4 book ai didi

ruby - 为什么Ruby中没有 `.split!`?

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

即使有 downcase!,使用它似乎也很合乎逻辑。还有其他人在 Ruby 中遇到过这个用例吗?

出于好奇,我正在尝试这样做:

def some_method(foo)
foo.downcase!.split!(" ")
## do some stuff with foo later. ##
end

some_method("A String like any other")

取而代之的是:

 def some_method(foo)
foo = foo.downcase.split(" ")
## do some stuff with foo later. ##
end

some_method("A String like any other")

这没什么大不了的......但是 ! 看起来更酷。

最佳答案

Why is there no .split! in Ruby?

It just seems pretty logical to have it when there's even a downcase!.

这可能是合乎逻辑的,但这是不可能的:对象不能更改它们在 Ruby 中的类或它们的身份。您可能会想到 Smalltalk 的 become:,它在 Ruby 中不存在也不能存在。 become: 改变对象的身份,因此也可以改变它的类。

关于ruby - 为什么Ruby中没有 `.split!`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39442774/

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