访问类'来自外部的实例变量

发布于 2024-10-02 21:10:35 字数 206 浏览 7 评论 0原文

我理解(我认为)Ruby 中类的类变量和实例变量之间的区别。

我想知道如何从该类的外部访问该类的实例变量。

从内部(即在类方法而不是实例方法中)可以直接访问它,但是从外部,有没有办法做到 MyClass.class.[@$#]variablename

我没有任何具体的理由这样做,只是学习 Ruby 并想知道这是否可能。

I understand (I think) the difference between class variables and instance variables of a class in Ruby.

I'm wondering how one can access the instance variables of a class from OUTSIDE that class.

From within (that is, in a class method instead of an instance method), it can be accessed directly, but from outside, is there a way to do MyClass.class.[@$#]variablename?

I don't have any specific reason for doing this, just learning Ruby and wondering if it is possible.

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

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

发布评论

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

评论(3

一袭水袖舞倾城 2024-10-09 21:10:40

在 ruby​​ 中,您可以通过两种方式

  1. 手动定义 getter 和 setter
  2. 使用 attr_* 方法

来实现此目的让我为您详细说明上述方法,

手动定义 getter 和 setter

class Human
  def sex=(gender)
    @sex = gender
  end

  def sex
    @sex
  end
end

//from outside  class 
human = Human.new
// getter method call 
puts human.sex
// setter method call to explicitly set the instance variable 
human.sex = 'female'

puts human.sex 
// now this prints female which is set

使用 attr_* 方法

class Human
  attr_accessor :sex
end

//from outside 

human = Human.new
// getter method call 
puts human.sex
// setter method call to explicitly set the instance variable 
human.sex = 'female'

puts human.sex 
// now this prints female which is set 

attr_accessor 在内部为您创建 setter 和 getter 方法,如果您只需要 setter,则可以使用 attr_writer,如果您只需要 getter,则可以使用 attr_reader。

我希望我回答了你的问题

In ruby you can achieve this in 2 ways

  1. manually defining getter and setters
  2. using attr_* methods

Let me elaborate the above ways for you,

Manually defining getter and setters

class Human
  def sex=(gender)
    @sex = gender
  end

  def sex
    @sex
  end
end

//from outside  class 
human = Human.new
// getter method call 
puts human.sex
// setter method call to explicitly set the instance variable 
human.sex = 'female'

puts human.sex 
// now this prints female which is set

using attr_* methods

class Human
  attr_accessor :sex
end

//from outside 

human = Human.new
// getter method call 
puts human.sex
// setter method call to explicitly set the instance variable 
human.sex = 'female'

puts human.sex 
// now this prints female which is set 

attr_accessor internally creates setter and getter methods for you, if you want only setter you can just use attr_writer and if you want only getter you can use attr_reader.

I hope, I answered your question

并安 2024-10-09 21:10:38

Ruby 有类、类对象和实例。

类变量属于类。
类实例变量
属于类对象

变量:

可在类及其实例中访问。
attr_accessor 不适用于类变量。

类实例变量:

只能通过类访问。
如果您在类中而不是在类对象中定义 attr_accessor ,则它会起作用,如下所示。

class A
    @b = 1
    class << self
        attr_accessor :b
    end
end

在类实例变量 b 的实例上定义 getter 和 setter:

class A
    @b = 1
    class << self
        attr_accessor :b
    end
    def b
        A.b
    end
    def b=(value)
        A.b=value
    end
end

现在可以通过所有者类及其实例来访问类实例变量 b。
作为一个几天前的 Ruby 学习者,这是我能做的最多的事情。

`irb(main):021:0* class A
irb(main):022:1> @b = 1
irb(main):023:1> class << self
irb(main):024:2> attr_accessor :b
irb(main):025:2> end
irb(main):026:1> def b
irb(main):027:2> A.b
irb(main):028:2> end
irb(main):029:1> def b=(v)
irb(main):030:2> A.b=v
irb(main):031:2> end
irb(main):032:1> end
=> :b=
irb(main):033:0> A.b
=> 1
irb(main):034:0> c = A.new
=> #<A:0x00000003054440>
irb(main):035:0> c.b
=> 1
irb(main):036:0> c.b= 50
=> 50
irb(main):037:0> A.b
=> 50
irb(main):038:0>`

是的,我开始不喜欢 ruby​​...iso 是一个更好的解决方案。

Ruby has Class, Class Object, and Instance.

A Class variable belongs to a Class.
A Class instance variable
belongs to a Class Object

Class variable:

Accessible within the class and its instances.
attr_accessor does not work on class variables.

Class instance variable:

Accessible only through the Class.
attr_accessor works if you define it in the class and not in the class object as below.

class A
    @b = 1
    class << self
        attr_accessor :b
    end
end

Defining a getter and setter on the instances for the class instance variable b in:

class A
    @b = 1
    class << self
        attr_accessor :b
    end
    def b
        A.b
    end
    def b=(value)
        A.b=value
    end
end

Now the class instance variable b can be accessed via the owner Class and its instances.
As a several days old ruby learner, this is the most I can do.

`irb(main):021:0* class A
irb(main):022:1> @b = 1
irb(main):023:1> class << self
irb(main):024:2> attr_accessor :b
irb(main):025:2> end
irb(main):026:1> def b
irb(main):027:2> A.b
irb(main):028:2> end
irb(main):029:1> def b=(v)
irb(main):030:2> A.b=v
irb(main):031:2> end
irb(main):032:1> end
=> :b=
irb(main):033:0> A.b
=> 1
irb(main):034:0> c = A.new
=> #<A:0x00000003054440>
irb(main):035:0> c.b
=> 1
irb(main):036:0> c.b= 50
=> 50
irb(main):037:0> A.b
=> 50
irb(main):038:0>`

Yes, I'm begining to dislike ruby...iso a better solution.

祁梦 2024-10-09 21:10:37
class MyClass

    @my_class_instance_var = "foo"

    class << self
        attr_accessor :my_class_instance_var
    end

end

puts MyClass::my_class_instance_var

前述结果:

>> foo

我相信 Arkku 演示了如何从类外部访问类变量 (@@),而不是类实例 变量 (@)。

我从这篇文章中得出了上述内容:清楚地看到元类

class MyClass

    @my_class_instance_var = "foo"

    class << self
        attr_accessor :my_class_instance_var
    end

end

puts MyClass::my_class_instance_var

The foregoing yields:

>> foo

I believe that Arkku demonstrated how to access class variables (@@), not class instance variables (@) from outside the class.

I drew the foregoing from this essay: Seeing Metaclasses Clearly

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