Skip to content

Instantly share code, notes, and snippets.

@ffloyd
Created April 20, 2014 08:44
Show Gist options
  • Save ffloyd/11108897 to your computer and use it in GitHub Desktop.
Save ffloyd/11108897 to your computer and use it in GitHub Desktop.
Example of runtime class generation
module MetaCL
module Error
class MetaCLError < StandardError; end
{
# matrices errors
MatrixUnknownElementType: 'Cannot define matrix: unknown element type',
MatrixInvalidSizeParams: 'Cannot define matrix: invalid size params',
MatrixNameDuplication: 'Cannot define matrix: matrix with same name already exists',
MatrixNotFound: 'Cannot find matrix with given name', # TODO: what name?
MatrixMismatchSizes: 'Mismatch sizes of matrices'
}.each do |class_name, message|
MetaCL::Error.const_set class_name, Class.new(MetaCLError)
MetaCL::Error.const_get(class_name).const_set('MESSAGE', message)
MetaCL::Error.const_get(class_name).class_eval <<CODE
def message
MESSAGE
end
CODE
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment