Ruby:了解更多有关为自我分配价值的参考资料

发布于 2024-12-08 20:58:16 字数 565 浏览 2 评论 0原文

我无法找到有关此主题的文档或任何参考资料: Ruby:如何编写一个 bang 方法,例如 map?

有人知道我可以阅读的任何内容来了解​​有关此特定的更多信息吗?

编辑:根据评论,我将这个问题修改如下:

因此,我们发现可以通过 self 的数组表示法来操作数组和字符串:

self[i]=

但这并不是操作背后的全部故事自我的价值。关于 self 的范围及其在当前上下文中的含义有很多参考资料,但我发现的关于 self 操作方法的信息并不多。

如果我想编写自己版本的 Stringchomp! 或其他 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 技术交流群。

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

发布评论

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

评论(2

小瓶盖 2024-12-15 20:58:16

首先阅读维基百科中关于 self 的文章(即使它没有提到 Ruby)全部)。

长话短说:

  • Ruby 借用了很多其他语言的概念,而 self 来自 Smalltalk。
  • self 在 Smalltalk 中被称为伪变量,这意味着它是变量,但它是由运行时环境设置的,而不是由程序或程序员设置的。
  • self 始终引用消息的接收者。 super 引用该消息的超类,该消息是由引用 super 所在的方法实现的。(很高兴您没有要求 super) 。
  • Ruby 中的 self(如 Smalltalk)始终引用当前对象,并且该对象可能是类的实例,甚至是类本身。因此,如果您在类端定义方法(只能在类上调用),即使有 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:

  • Ruby has borrowed a lot of concepts from other languages, and 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 reference super is in. (Glad that you did not ask for super).
  • 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 there self references the object, which is the class. So it is possible in Ruby to use only self, 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.

っ〆星空下的拥抱 2024-12-15 20:58:16

(因为评论有点长...)

确实,您无法更改 self 的值,但您可以更改 self 的属性,即你的例子中发生了什么。

让我详细说明一下。假设你有一个类 Foo 并且你做了这样的事情:

f = Foo.new
f.bar = 3
puts f.bar # => 9

2”?这里实际发生的情况是,您使用参数 1 调用 f 上的 bar= 方法。方法 bar= 可能看起来像这样:

class Foo
  def bar=(val)
    @bar = val**2 # square the given value and assign it to the instance
  end             # variable @bar

  def bar
    @bar          # return the instance variable @bar -- a shortcut for this is
  end

  # we could get rid of the second method, though, but using attr_reader:
  attr_reader :bar
end

好的,那么这个怎么样?

f = Foo.new
puts f[5] # => 10

10”?!是的。同样,[] 只是普通旧方法的语法糖。像这样的:

class Foo
  def [](val)
    val * 2 # Ruby just takes the value you put between [] and gives it to you as
  end       # the first parameter
end

最后:

f = Foo.new
f[:bar] = 99
puts f[:bar] # => 100

是的,你猜对了,这只是另一个方法调用。例如:

class Foo
  @my_hash = {}

  def []=(key, val)         # Ruby gives us the value between the [] as the first
    @my_hash[key] = val + 1 # parameter and the value after the = as the second,
  end                       # and we use them to set a value on an internal
                            # instance variable...
  def [](key)
    @my_hash[key]           # ...and we can use the "getter" to get a value from 
  end                       # the instance variable.
end

你是对的,这些内容并没有全部包含在一个方便的来源中,所以我希望这会有所帮助。如果您需要进一步的解释,请随时发表评论。

(Since this is a little long for a comment...)

Indeed, you can't change the value of self, but you can change properties on self, which is what's happening in your example.

Let me elaborate. Say you have a class Foo and you do something like this:

f = Foo.new
f.bar = 3
puts f.bar # => 9

"2"?? What's actually happening here is that you're calling a method bar= on f with the argument 1. The method bar= could look something like this:

class Foo
  def bar=(val)
    @bar = val**2 # square the given value and assign it to the instance
  end             # variable @bar

  def bar
    @bar          # return the instance variable @bar -- a shortcut for this is
  end

  # we could get rid of the second method, though, but using attr_reader:
  attr_reader :bar
end

Okay, so what about this?

f = Foo.new
puts f[5] # => 10

"10"?! Yep. Again, [] is just syntactic sugar for a plain old method. Something like this:

class Foo
  def [](val)
    val * 2 # Ruby just takes the value you put between [] and gives it to you as
  end       # the first parameter
end

And finally:

f = Foo.new
f[:bar] = 99
puts f[:bar] # => 100

Yep, you guessed it, this is just another method call. For example:

class Foo
  @my_hash = {}

  def []=(key, val)         # Ruby gives us the value between the [] as the first
    @my_hash[key] = val + 1 # parameter and the value after the = as the second,
  end                       # and we use them to set a value on an internal
                            # instance variable...
  def [](key)
    @my_hash[key]           # ...and we can use the "getter" to get a value from 
  end                       # the instance variable.
end

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.

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