如何为Ruby中的所有实例设置默认(而不是nil)变量初始化

时间:2022-10-14 00:24:25

Where is instance variable initialized as nil first time? Can I redefine it as other value by default for all instances?

实例变量在哪里初始化为nil?对于所有实例,我可以默认将其重新定义为其他值吗?

For example:

class Class
  #some code here or maybe in an Object class
end

class Foo1
  attr_accessor :bar
end

class Foo2
  attr_accessor :bar
end

p Foo1.new.bar # result is not nil
p Foo2.new.bar # result is not nil

This can be done by modifying the reader:

这可以通过修改阅读器来完成:

class Class
  def attr_accessor(attr_name)
    ...
    define_method "#{attr_name}" do
      if instance_variable_get "@#{attr_name}_history"
        instance_variable_get "@#{attr_name}_history"
      else
        "Not nil"
      end
    end
    ...
  end
end

But this doesn't help in understanding the core of Ruby. Many thanks!

但这无助于理解Ruby的核心。非常感谢!

3 个解决方案

#1


0  

Define a new method in class Class. Get instance variables through :instance_variables and set them to anything you like by using :instance_variable_set(:@var,default_value)

在类Class中定义一个新方法。通过:instance_variables获取实例变量,并使用以下命令将它们设置为您喜欢的任何内容:instance_variable_set(:@ var,default_value)

class Class

    alias oldNew new
        def new(*args)
        result = oldNew(*args)
        default = 2354 # set default here
        a = result.instance_variables
        a.each do
        |d|
        result.instance_variable_set(d,default)
        end

    return result
    end

end

#2


3  

If you want to set default values, you can assign them in an initialize method of a class.

如果要设置默认值,可以在类的initialize方法中指定它们。

For example:

class Test

  attr_accessor :bar

  def initialize
    @bar = 'bar'
  end

end

Test.new.bar
# => "bar"

Remember that attr_accessor :bar gives you helper methods to set and get the underlying instance variable @bar.

请记住,attr_accessor:bar为您提供了帮助方法来设置和获取底层实例变量@bar。

If you want default values for lots of classes, you can have them inherit from a class that sets the instance variables as not nil:

如果你想要许多类的默认值,你可以让它们从一个将实例变量设置为nil的类继承:

class Foo < Test

end

Foo.new.bar
# => "bar"

#3


0  

(Corrected following Jörg W Mittag's comment)

(更正了JörgWMittag的评论)

No you cannot. Instance variables are set evaluated to nil when you call them without assigning a value to them.

你不能。当您调用实例变量而不为其赋值时,将实例变量设置为nil。

#1


0  

Define a new method in class Class. Get instance variables through :instance_variables and set them to anything you like by using :instance_variable_set(:@var,default_value)

在类Class中定义一个新方法。通过:instance_variables获取实例变量,并使用以下命令将它们设置为您喜欢的任何内容:instance_variable_set(:@ var,default_value)

class Class

    alias oldNew new
        def new(*args)
        result = oldNew(*args)
        default = 2354 # set default here
        a = result.instance_variables
        a.each do
        |d|
        result.instance_variable_set(d,default)
        end

    return result
    end

end

#2


3  

If you want to set default values, you can assign them in an initialize method of a class.

如果要设置默认值,可以在类的initialize方法中指定它们。

For example:

class Test

  attr_accessor :bar

  def initialize
    @bar = 'bar'
  end

end

Test.new.bar
# => "bar"

Remember that attr_accessor :bar gives you helper methods to set and get the underlying instance variable @bar.

请记住,attr_accessor:bar为您提供了帮助方法来设置和获取底层实例变量@bar。

If you want default values for lots of classes, you can have them inherit from a class that sets the instance variables as not nil:

如果你想要许多类的默认值,你可以让它们从一个将实例变量设置为nil的类继承:

class Foo < Test

end

Foo.new.bar
# => "bar"

#3


0  

(Corrected following Jörg W Mittag's comment)

(更正了JörgWMittag的评论)

No you cannot. Instance variables are set evaluated to nil when you call them without assigning a value to them.

你不能。当您调用实例变量而不为其赋值时,将实例变量设置为nil。