开发者

Ruby: Reference materials to learn more about assigning values to self

开发者 https://www.devze.com 2023-04-11 04:36 出处:网络
I haven\'t been able to find documentation or any reference material on this topic: Ruby: How to write a bang method, like map?开发者_运维问答

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!


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.


(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号