Declaring instance variables iterating over a hash!

前端 未结 4 1436
忘掉有多难
忘掉有多难 2021-02-02 16:43

i want to do the following:

I want to declare the instance variables of a class iterating over a dictionary.

Let\'s assume that i have this hash

         


        
4条回答
  •  花落未央
    2021-02-02 17:03

    class MyClass
      def initialize()
        hash = {"key1" => "value1","key2" => "value2","key3" => "value3"}
        hash.each do |k,v|
          instance_variable_set("@#{k}",v)
          # if you want accessors:
          eigenclass = class<

    The eigenclass is a special class belonging just to a single object, so methods defined there will be instance methods of that object but not belong to other instances of the object's normal class.

提交回复
热议问题