Dynamic constant assignment

后端 未结 7 818
眼角桃花
眼角桃花 2020-11-28 20:31
class MyClass
  def mymethod
    MYCONSTANT = \"blah\"
  end
end

gives me the error:

SyntaxError: dynamic constant assignmen

7条回答
  •  情书的邮戳
    2020-11-28 21:00

    Because constants in Ruby aren't meant to be changed, Ruby discourages you from assigning to them in parts of code which might get executed more than once, such as inside methods.

    Under normal circumstances, you should define the constant inside the class itself:

    class MyClass
      MY_CONSTANT = "foo"
    end
    
    MyClass::MY_CONSTANT #=> "foo"
    

    If for some reason though you really do need to define a constant inside a method (perhaps for some type of metaprogramming), you can use const_set:

    class MyClass
      def my_method
        self.class.const_set(:MY_CONSTANT, "foo")
      end
    end
    
    MyClass::MY_CONSTANT
    #=> NameError: uninitialized constant MyClass::MY_CONSTANT
    
    MyClass.new.my_method
    MyClass::MY_CONSTANT #=> "foo"
    

    Again though, const_set isn't something you should really have to resort to under normal circumstances. If you're not sure whether you really want to be assigning to constants this way, you may want to consider one of the following alternatives:

    Class variables

    Class variables behave like constants in many ways. They are properties on a class, and they are accessible in subclasses of the class they are defined on.

    The difference is that class variables are meant to be modifiable, and can therefore be assigned to inside methods with no issue.

    class MyClass
      def self.my_class_variable
        @@my_class_variable
      end
      def my_method
        @@my_class_variable = "foo"
      end
    end
    class SubClass < MyClass
    end
    
    MyClass.my_class_variable
    #=> NameError: uninitialized class variable @@my_class_variable in MyClass
    SubClass.my_class_variable
    #=> NameError: uninitialized class variable @@my_class_variable in MyClass
    
    MyClass.new.my_method
    MyClass.my_class_variable #=> "foo"
    SubClass.my_class_variable #=> "foo"
    

    Class attributes

    Class attributes are a sort of "instance variable on a class". They behave a bit like class variables, except that their values are not shared with subclasses.

    class MyClass
      class << self
        attr_accessor :my_class_attribute
      end
      def my_method
        self.class.my_class_attribute = "blah"
      end
    end
    class SubClass < MyClass
    end
    
    MyClass.my_class_attribute #=> nil
    SubClass.my_class_attribute #=> nil
    
    MyClass.new.my_method
    MyClass.my_class_attribute #=> "blah"
    SubClass.my_class_attribute #=> nil
    
    SubClass.new.my_method
    SubClass.my_class_attribute #=> "blah"
    

    Instance variables

    And just for completeness I should probably mention: if you need to assign a value which can only be determined after your class has been instantiated, there's a good chance you might actually be looking for a plain old instance variable.

    class MyClass
      attr_accessor :instance_variable
      def my_method
        @instance_variable = "blah"
      end
    end
    
    my_object = MyClass.new
    my_object.instance_variable #=> nil
    my_object.my_method
    my_object.instance_variable #=> "blah"
    
    MyClass.new.instance_variable #=> nil
    

提交回复
热议问题