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','url':'http://stackoverflow.com/questions/6712298/dynamic-constant-assignment','og_descr':'class MyClass def mymethod MYCONSTANT = \'blah\' end end gives me the error: SyntaxError: dynamic constant assignment error Why is this considered a dynamic constant? I'm just assigni...
. dynamic constant assignment error Why is this considered a dynamic constant? dynamic constant assignment error. Dynamic constant assignment Ruby.
. dynamic constant assignment Console_Screen = Screen.new ^ typechallenge.rb:90: dynamic constant assignment Typing_Test = Test.new The script. constant (dynamic.
Ruby Test Dynamic Constant Assignments
>> I guess that the 'dynamic constant assignment' means that you can assing >> to a >> constant in a method. > > > And the work around is > def self.set(value).
Ruby merge.rb. merge.rb:8: dynamic constant assignment. Li=Array.new(n1.object_id) ^ merge.rb:9: dynamic constant assignment. Ri=Array.
Ruby Test Dynamic Constant Assignment Satisfaction
. dynamic constant assignment QB_estimate_number = local_assigns[. Otherwise Ruby interprets > them as a > constant. So how do you create a local constant?