Created
April 20, 2014 08:44
-
-
Save ffloyd/11108897 to your computer and use it in GitHub Desktop.
Example of runtime class generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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