难以理解 Ruby class 的 initialize(constructor) 中使用 self 和 @ 的区别

Trouble in understanding the difference between the use of self and @ in initialize(constructor) of Ruby class

我正在阅读 "Well Grounded Rubyist" 这本书,我希望能够澄清我对 self 与 @ 初始化的理解。

有时您可以使用 "self." 创建简单的门票 class 来设置地点和日期。概括地说,我知道 "self." 正在为当前 Ticket 实例设置地点和日期,而 @ 正在设置一个实例变量……但我不太确定为什么不使用实例变量呢?

从只是玩弄它来看,两者似乎都是有效的,因为我可以使用 @ 或 self 进行初始化,在新的 Ticket 实例上成功访问日期或地点。

class Ticket
  attr_accessor :venue, :date
  def initialize(venue, date)
    self.venue = venue
    self.date = date
  end
end

如果能帮助我澄清我的理解,将不胜感激!

当您使用 attr_accessor :venue 时,您实际上就是在这样做。

def venue
  @venue
end 

def venue=(value)
  @venue = value
end 

当你做 attr_writer :venue 时,你正在做

def venue=(val)
  @venue = val 
end 

当你做 attr_reader :venue 时,你正在做

def venue
  @venue 
end

请注意 @instance_variable 默认为 nil,或者如果它未初始化。要查看实际效果,请在您的终端中键入 irb 并按回车键。然后,键入一个 @,并在其后发送一堆字符。 @ausdhyf934234092348 将为零。

当您在 Ticket#initialize 中时,您将充当对象的一个​​实例,因此 self 将 return 当前实例。 self.class 将 return class,并让您访问任何 class 级别的方法。

class Ticket
  VENUE_SIZES = [:small, :large]

  def self.venue_sizes
    VENUE_SIZES
  end 

  def initialize(venue, date)
    # ... 
    self.venue_sizes # NoMethodError
    self.class.venue_sizes # [:small, :large]
  end 

end

向上链,如果您充当 class 而不是 class 的实例,则调用 self.class — 或者 self.class.class 如果您作为实例,将 return Class

attr_accessor 只是语法糖;它的创建是因为必须一遍又一遍地编写那些 getter/setter 方法的平凡琐事。有更酷的问题需要解决,比如在 Ruby 中编写我们自己的 nadnerb_accessor (尽管 attr_accessor 是在 C 中实现的):

class Ticket
  def self.nadnerb_accessor(*names)
    names.each do |name|
      define_method(name) do
        instance_variable_get(:"@#{name}")
      end
      define_method("#{name}=") do |value|
        instance_variable_set(:"@#{name}", value)
      end
    end
  end

  nadnerb_accessor :venue, :price

  def initialize(venue, price)
    self.venue = venue
    self.price = price
  end

  def inspect
    "#<Ticket @venue=\"#{@venue}\" @price=\"#{@price}\">"
  end
end

ticket = Ticket.new("Whosebug", "")
puts ticket.inspect 

几乎相同,但一个被视为变量 (@),另一个被视为方法(感谢 getter 和 setter)。 示例 4 "unveils the trick".

我希望以下示例可以不言自明:


示例 1

class Ticket

  attr_accessor :venue

  def initialize(venue)
    self.venue = venue
  end

  def are_the_same?
    p self.venue == @venue
    p self.venue === @venue
    p self.venue.eql? @venue
    p self.venue.equal? @venue
  end

end

ticket = Ticket.new('Broadway') # this is an instance of Ticket

ticket.are_the_same?

#=> true
#=> true
#=> true
#=> true

See this great post for equals.


示例 2

class Ticket

  @@double_arroba = 'class variable' # class variable
  attr_accessor :venue

  def initialize(venue)
    self.venue = venue
  end

  def whats_self?
    p self # instance itself
  end

  def venue_self
    p self.venue # instance itself  calling its variable
  end

  def venue_arroba
    p @venue # instance variable directly
  end

  def whats_double_arroba # instance method using a class variable
    p @@double_arroba
  end

  def self.whats_double_arroba # class method using a class variable
    p @@double_arroba
  end

  def self.whats_self? # class method calling itself
    p self
  end

end

ticket = Ticket.new('Broadway') # this is an instance of Ticket

p ticket #=> #<Ticket:0x007fb3fb0f1868 @venue="Broadway">
ticket.whats_self? #<Ticket:0x007fb3fb0f1868 @venue="Broadway">
p ticket.venue #=> "Broadway" #thanks to attr_accessor
ticket.venue_self #=> "Broadway"
ticket.venue_arroba #=> "Broadway"

ticket.whats_double_arroba #=> "class variable"
Ticket.whats_double_arroba #=> "class variable"
Ticket.whats_self? #=> Ticket

示例 3

如果删除 attr_accessor 会怎样?

class Ticket

  # attr_accessor :venue #comment out attr_accessor

  def initialize(venue)
    # self.venue = venue # venue method is not defined
    @venue = venue # so you need to use @
  end

  def venue_self
    # p self.venue # not working, self method is not available is nota available
  end

  def venue_arroba
    p @venue # instance variable directly
  end

end

ticket = Ticket.new('Broadway') # this is an instance of Ticket

p ticket #=> #<Ticket:0x007fb3fb0f1868 @venue="Broadway">
# p ticket.venue # does not work, no access
ticket.venue_self #=> nil, because commented the method code
ticket.venue_arroba #=> "Broadway"
# ticket.whats_double_arroba # not working

示例 4

诀窍:getter和setter

# class with getter and setter

class Ticket

  def venue=(value) # setter (attr_reader)
    @venue = value
  end

  def venue # getter (attr_writer)
    @venue
  end

  def self_venue
    self.venue # (*) you can omit self and write just venue
  end

end

ticket = Ticket.new # ticket is the instance
ticket.venue="Brodway" # setter method .venue= in action
p ticket.venue #=> "Brodway" # getter method .venue in action
p ticket.self_venue #=> "Brodway" # calls the method .self_venue on the instance, which calls the getter method .venue (*)


# or write simply this code below, to let ruby build itself getter and setter

class Ticket

  attr_accessor :venue

end