Skip to content

Instantly share code, notes, and snippets.

@plukevdh
Created April 26, 2011 03:11
Show Gist options
  • Save plukevdh/941725 to your computer and use it in GitHub Desktop.
Save plukevdh/941725 to your computer and use it in GitHub Desktop.
A nonsensical example of what I want to do. Someone please fill in the blanks.
class Parser
class << Base
# what to do here?
def create(sym)
eval 'Parser::#{sym.to_s.capitalize}.new'
end
end
end
class Parser::Html < Parser::Base
end
class Parser::Pdf < Parser::Base
end
parser = Parser.create(type: :html)
parser.class
#> Parser::Base::Html
@justinbaker999
Copy link

Are you planning on many parsers, or just a small subset? and there is a better way than eval

Object.get_const("Parser::#{sym.to_s.capitalize}").new

@plukevdh
Copy link
Author

yeah, that's helpful. definitely planning on many parsers. I want it to be extendable so others can write plugins as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment