Create a class dynamically : clone « Class « Ruby






Create a class dynamically



material = 'cotton'
class << material
  def definition
     puts 'this is a test.'
  end
end

puts material.definition

'cotton'.definition
# NoMethodError: undefined method 'definition' for "cotton":String

material.clone.definition


material.dup.definition
# NoMethodError: undefined method 'definition' for "cotton":String

 








Related examples in the same category

1.Making a Copy of an Object
2.Clone a customized string