S
Samnang Chhun
I am learning how to design a DSL by using const_missing, but I face a
problem on getting class name:
class Drawing
def initialize(&block)
instance_eval(&block)
end
def self.const_missing(constant)
klass = Class.new do
def self.<(super_klass)
puts "#{self.name} inherits from #{super_klass.name}"
end
def self.-(association_klass)
puts "#{self.name} has association with
#{association_klass.name}"
end
end
const_set(constant, klass)
end
end
Drawing.new do
Student < Person
Student - Courses
end
Output:
inherits from
has association with
Expected output:
Student inherits from Person
Student has association with Courses
problem on getting class name:
class Drawing
def initialize(&block)
instance_eval(&block)
end
def self.const_missing(constant)
klass = Class.new do
def self.<(super_klass)
puts "#{self.name} inherits from #{super_klass.name}"
end
def self.-(association_klass)
puts "#{self.name} has association with
#{association_klass.name}"
end
end
const_set(constant, klass)
end
end
Drawing.new do
Student < Person
Student - Courses
end
Output:
inherits from
has association with
Expected output:
Student inherits from Person
Student has association with Courses