Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[每日见闻-2017-07-20]关于self的小细节 #3

Closed
xiaohesong opened this issue Jul 20, 2017 · 1 comment
Closed

[每日见闻-2017-07-20]关于self的小细节 #3

xiaohesong opened this issue Jul 20, 2017 · 1 comment
Labels

Comments

@xiaohesong
Copy link
Owner

xiaohesong commented Jul 20, 2017

  • self在当前的initialize中直指当前的实例对象
class Foo
  def initialize(x)
    self.c x
  end
 
  def c(x)
    puts "c的参数是#{x}"
  end
end
f = Foo.new(1)
  • 带给我的感觉
    self在很大程度上类似.都是指定当前的实例.给我的感觉就是
self == @
!self.eql?(@)

类似于coffee js

this && @

例如:

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

再比如

class A
  @num = 8
  def show
    puts @num
  end
end

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

b = B.new
b.show
@xiaohesong
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant