了解 attr_accessor 类方法的 self

发布于 2024-11-06 19:43:46 字数 949 浏览 0 评论 0原文

class Test
  class << self
    attr_accessor :some

    def set_some
      puts self.inspect
      some = 'some_data'
    end
    def get_some
      puts self.inspect
      some
    end
  end
end

Test.set_some => Test
puts Test.get_some.inspect => Test nil

上面我可以找到 self 作为 Test 本身,但不会返回 some_data 作为输出。

但是,当我按以下方式修改时,它返回预期输出

class Test
  class << self
    attr_accessor :some

    def set_some
      puts self.inspect
      self.some = 'some_data'
    end
    def get_some
      puts self.inspect
      self.some
    end
  end
end

Test.set_some => Test
puts Test.get_some.inspect => Test some_data

有什么区别?

编辑

现在,在第一个示例中,如果我将 get some 方法设置为

Test.some = 'new_data'
puts Test.some.inspect #=> new_data
Test.set_some
puts Test.get_some.inspect => new_data

Now,它会让我更加困惑。

class Test
  class << self
    attr_accessor :some

    def set_some
      puts self.inspect
      some = 'some_data'
    end
    def get_some
      puts self.inspect
      some
    end
  end
end

Test.set_some => Test
puts Test.get_some.inspect => Test nil

Here above I could find self as Test itself but not returning the some_data as output.

But while I modified in following way it returns expected output

class Test
  class << self
    attr_accessor :some

    def set_some
      puts self.inspect
      self.some = 'some_data'
    end
    def get_some
      puts self.inspect
      self.some
    end
  end
end

Test.set_some => Test
puts Test.get_some.inspect => Test some_data

What is the differences?

EDIT

Now in the first example if I set as get some method as

Test.some = 'new_data'
puts Test.some.inspect #=> new_data
Test.set_some
puts Test.get_some.inspect => new_data

Now it made me much more confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

脸赞 2024-11-13 19:43:46

some = :foo 使 ruby​​ 认为它应该创建一个名为 some 的新局部变量。如果您想调用 some=(),则必须使用显式接收器 - 如 self.some = :foo 中所示。我曾经打赌输过......:-/

some = :foo makes ruby think it should create a new local variable with name some. If you want to call some=(), you have to use an explicit reciever - as in self.some = :foo. I once lost a bet on that... :-/

dawn曙光 2024-11-13 19:43:46

在第一个示例中它是(局部)变量

It's (local) variable in the first example

Smile简单爱 2024-11-13 19:43:46

在第一个示例中,some 是一个局部变量。

在第二个中,someself 的一种方法。为什么?因为 attr_accessor :some 与:

def some= (val)
  @some = val
end

def some
  return @some
end

所以,您已经为实例变量 @some 创建了 getter 和 setter 方法(它是对象 的实例变量)测试,因为每个类也是类Class 的对象)。

In the first example some is a local variable.

In the second one, some is a method of self. Why? Because attr_accessor :some is the same as:

def some= (val)
  @some = val
end

def some
  return @some
end

So, you have created the getter and setter methods for the instance variable @some (it's an instance variable of the object Test, as every class is also an object of class Class).

日记撕了你也走了 2024-11-13 19:43:46

在第一个方法中,

def set_some
  puts self.inspect
  some = 'some_data'
end

some 是局部变量。它与 @some 不同,@some 是实例变量(在本例中是类实例变量),因此该值在方法结束时消失。

如果您想调用 setter 方法 some 或将 @some 设置为某些内容,请执行此操作

@some = 'some_data'

self.some = 'some_data'

在第二个方法中

def get_some
  puts self.inspect
  self.some
end

调用该方法 some 。它返回实例变量@some..并且因为此时@some没有值..返回nil..

in the first method

def set_some
  puts self.inspect
  some = 'some_data'
end

some is a local variable.. its not the same as @some that is a instance variable (in this case a class instance variable) so the value disappears when the method ends.

if you want to call the setter method some or set @some to something then do this

@some = 'some_data'

or

self.some = 'some_data'

in the second method

def get_some
  puts self.inspect
  self.some
end

your calling the method some. which returns the instace variable @some.. and since at this point @some has no value.. returns nil..

九八野马 2024-11-13 19:43:46

示例 1 没有方法重写,也没有局部变量

class Foo
  def initialize
    @foo = 'foo'
  end

  def print_foo
    print @foo
    print self.foo
    print foo
  end
end

@foo、self.foo 和 foo 将在实例方法中访问实例变量 @foo:

<块引用>

Foo.new.print_foo #=> foofoofoo

示例 2 使用方法重写

class Foo
  def initialize
    @foo = 'foo'
  end

  def foo
    return 'bar'
  end

  def print_foo
    print @foo
    print self.foo
    print foo
  end
end

@foo 将访问实例变量,但 self.foo 和 foo 将调用 foo 重写方法:

<块引用>

Foo.new.print_foo #=> foob​​arbar

示例 3 具有方法重写和局部变量

class Foo
  def initialize
    @foo = 'foo'
  end

  def foo
    return 'bar'
  end

  def print_foo
    foo = 'baz'
    print @foo
    print self.foo
    print foo
  end
end

@foo 访问实例变量,self.foo 访问重写方法,foo 访问局部变量:

<块引用>

Foo.new.print_foo #=>福巴巴兹

Example 1 with no method override and no local variable

class Foo
  def initialize
    @foo = 'foo'
  end

  def print_foo
    print @foo
    print self.foo
    print foo
  end
end

@foo, self.foo, and foo will access instance variable @foo within the instance method:

Foo.new.print_foo #=> foofoofoo

Example 2 with method override

class Foo
  def initialize
    @foo = 'foo'
  end

  def foo
    return 'bar'
  end

  def print_foo
    print @foo
    print self.foo
    print foo
  end
end

@foo will access the instance variable, but self.foo and foo will call the foo override method:

Foo.new.print_foo #=> foobarbar

Example 3 with method override and local variable

class Foo
  def initialize
    @foo = 'foo'
  end

  def foo
    return 'bar'
  end

  def print_foo
    foo = 'baz'
    print @foo
    print self.foo
    print foo
  end
end

@foo accesses instance variable, self.foo accesses override method, and foo accesses local variable:

Foo.new.print_foo #=> foobarbaz

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