Ruby:了解更多有关为自我分配价值的参考资料
我无法找到有关此主题的文档或任何参考资料: Ruby:如何编写一个 bang 方法,例如 map?
有人知道我可以阅读的任何内容来了解有关此特定的更多信息吗?
编辑:根据评论,我将这个问题修改如下:
因此,我们发现可以通过 self 的数组表示法来操作数组和字符串:
self[i]=
但这并不是操作背后的全部故事自我的价值。关于 self 的范围及其在当前上下文中的含义有很多参考资料,但我发现的关于 self 操作方法的信息并不多。
如果我想编写自己版本的 String
的 chomp!
或其他 bang 方法怎么办?我是否被限制使用 self[0]...self[i]
?其他班级呢?
谢谢!
I haven't been able to find documentation or any reference material on this topic: Ruby: How to write a bang method, like map?
Anyone know of anything I can read to learn more about this specific thing?
EDIT: In light of the comments, I'm amending this question as follows:
So, we discovered that Arrays and Strings can be manipulated through this array notation of self:
self[i]=
But that's not the whole story behind manipulating the value of self. There are plenty of reference materials about the scope of self and what it means in its current context, but there isn't much I've found about self
manipulation methods.
What if I wanted to write my own version of String
's chomp!
or other bang method? Am I locked into using self[0]...self[i]
? What about other classes?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先阅读维基百科中关于 self 的文章(即使它没有提到 Ruby)全部)。
长话短说:
self
来自 Smalltalk。self
在 Smalltalk 中被称为伪变量,这意味着它是变量,但它是由运行时环境设置的,而不是由程序或程序员设置的。self
始终引用消息的接收者。super
引用该消息的超类,该消息是由引用super
所在的方法实现的。(很高兴您没有要求super
) 。self
也会引用该对象,即类。因此,在 Ruby 中可以仅使用 self,您不必写下类的名称来表示接收者。这在重构时有点帮助。如果您已经了解了所有这些,请查看Metaprogramming Ruby,它会告诉您更多关于如何使用 self、类、特征类和其他一些有趣的东西的技巧。
First read the article in Wikipedia about self (even if it does not mention Ruby at all).
To make a long story short:
self
comes from Smalltalk.self
is called in Smalltalk a pseudo-variable, which means it is variable, but it is set by the runtime environment, not by the program or programmer.self
references all the time the receiver of a message.super
references the superclass of that message that is implemented by the method the referencesuper
is in. (Glad that you did not ask forsuper
).self
in Ruby (as in Smalltalk) references all the time the current object, and that may be an instance of a class or even a class itself. So if you define methods on the class-side (only callable on the class), even thereself
references the object, which is the class. So it is possible in Ruby to use onlyself
, you never have to write down the name of the class to denote the receiver. That helps a little bit when refactoring.If you have get all that, take a look at Metaprogramming Ruby which tells you some more tricks how to use
self
, classes, eigenclasses and some other interesting things.(因为评论有点长...)
确实,您无法更改
self
的值,但您可以更改self
的属性,即你的例子中发生了什么。让我详细说明一下。假设你有一个类
Foo
并且你做了这样的事情:“
2
”?这里实际发生的情况是,您使用参数1
调用f
上的bar=
方法。方法bar=
可能看起来像这样:好的,那么这个怎么样?
“
10
”?!是的。同样,[]
只是普通旧方法的语法糖。像这样的:最后:
是的,你猜对了,这只是另一个方法调用。例如:
你是对的,这些内容并没有全部包含在一个方便的来源中,所以我希望这会有所帮助。如果您需要进一步的解释,请随时发表评论。
(Since this is a little long for a comment...)
Indeed, you can't change the value of
self
, but you can change properties onself
, which is what's happening in your example.Let me elaborate. Say you have a class
Foo
and you do something like this:"
2
"?? What's actually happening here is that you're calling a methodbar=
onf
with the argument1
. The methodbar=
could look something like this:Okay, so what about this?
"
10
"?! Yep. Again,[]
is just syntactic sugar for a plain old method. Something like this:And finally:
Yep, you guessed it, this is just another method call. For example:
You're right, this stuff isn't all covered in one single, convenient source, so I hope this helps. Feel free to comment if you need further explanation.