LoginSignup
5
5

More than 5 years have passed since last update.

evalを使わない動的なクラス定義

Posted at
class_names = %w[Some Klass Name]

class_names.each do |name|
  self.class.const_set name, Class.new {|k|
    attr_accessor :attr
    def initialize
      @attr = :attr
    end
  }
end

class_names.each do |klass|
  p self.class.const_get(klass).new
end
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5