From: Vicente Bosch Campos Date: 2011-03-28T05:38:50+09:00 Subject: Re: Dynamic classes On Mar 27, 2011, at 9:02 PM, Eric Christopherson wrote: > On Sat, Mar 26, 2011 at 1:16 PM, Vicente Bosch Campos wrote: >> I have not explained my self properly. Imagine we are talking about making an instance of any other object e.g. a Car and the car has a name and it allows you reference the car instance by it in the future. >> >> Would you prefer the car name is defined by the variable that contains it or by setting the name in the constructor? >> >> hotwheels = Car.new >> >> current_car = Car.new("hotwheels") >> >> I don't think anyone would understand directly that I have automatically set a property inside my car with the variable name. It would be a bit counterintuitive and different from how any other instance properties work on this class and how they are usually set on any other type of object of any library. >> >> Considering the Class to be an object too by not allowing me set the name at the .new and taking the name of the left hand side of the equality sign in case it is a Constant ( otherwise it does not) it is not acting very much like any other object. > > Is there a way in Ruby to do this with objects generally (i.e. to > automatically set a name property upon assignment with =), not just > with Classes? > I think this Constant "Magic" only works for classes as Robert has indicated. At least I have not seen anything on google or my bookshelf to do it. I personally would not use it, its an exception of how the operator works, and forces people to delve into how your object works which can be considered as not very clean. For the specific case of the Class and being such a high level metaprogramming structure, love Ruby for it, I can understand why Matz treated it as something special.