# self

* `self`在当前的`initialize`中直指当前的实例对象

  ```ruby
  class Foo
  def initialize(x)
    self.c x
  end

  def c(x)
    puts "c的参数是#{x}"
  end
  end
  f = Foo.new(1)
  ```
* 带给我的感觉 `self`在很大程度上类似.都是指定当前的实例.给我的感觉就是

  ```ruby
  self == @
  !self.eql?(@)
  ```

  类似于`coffee js`的

  ```javascript
  this && @
  ```

  例如:

  \`\`\`ruby class Foo attr\_accessor :name def initialize(x) @name = "wo" end end

class Foo attr\_accessor :name def initialize(x) self.name = "wo" end end

f = Foo.new(3) puts f.name

````
再比如
```ruby
class A
  @num = 8
  def show
    puts @num
  end
end

class B < A
  def initialize
    @num = 3
  end
end

b = B.new
b.show
````


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaohesong.gitbook.io/today-i-learn/ruby/self.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
