0

I have a model source (name, url, method) The program parses the source's url for data. Now I have differen classes for parsing different types of urls: rss, xml, ogdata ...

So I have a common interface

new(url) //Parses the url

getTitle() //Get the title of the data

getPrice() //Get the price

And I have four classes rss, xml, ogdata. These classes are in seperate files. The idea is to add more classes as I discover different types of pages. I want to initiate the right classes for the url.

method=source.method //NOw I want to create a new class of the name specified in method

parser=Rss.new(source.url)//Instead of this //Something like parser=.new(source.url)

1

2 Answers 2

0

You can try using const_get.

Ex:-

Kernel.const_get("YourClassNameHere").new

*Kernel.const_get("YourClassNameHere")* will get you the class. And you can use it to instantiate new instances of the class.

Sign up to request clarification or add additional context in comments.

Comments

0
irb(main):081:0> User.name
=> "User"

then

"User".constantize.new(name: 'David')

or

User.name..constantize.new(name: 'David')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.