School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,122 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,988 people online right now. Registration is fast and FREE... Join Now!




Loading class from folder based on user input

 

Loading class from folder based on user input

tuntis

8 Oct, 2009 - 08:05 AM
Post #1

New D.I.C Head
*

Joined: 10 Nov, 2008
Posts: 22



Thanked: 1 times
My Contributions
This is something quite simple that I just don't know how to implement in the best way.

Let me explain better - I'm working on a project called risg: it's a program that parses IRC logfiles and generates fancy statistics based on them.

Now, it's obvious that it needs to support many kinds of logfile formats and output formats: you can see that I've implemented a system for both logfile formats and output formats (see lib/risg/format and lib/risg/output).

But there's one problem - I don't know how to allow the user to decide which format to use for logfiles and output. If I add a command line switch like "--format" to the main executable, how should I load and instiate the right class from the format directory based on user input?

And while I'm at it, I've been wondering if I should package the application as a Rubygem: is it an optimal way of deployment/distribution?

User is offlineProfile CardPM
+Quote Post


xclite

RE: Loading Class From Folder Based On User Input

8 Oct, 2009 - 08:44 AM
Post #2

LIKE A BOSS
****

Joined: 12 May, 2009
Posts: 655



Thanked: 14 times
My Contributions
Gems are nice.

As for selecting the correct format:
You could have a variable that will hold the log somehow.
Have some sort of conditional
CODE

if format1
    require "format1"
    logger = format1.new(args)
else
    require "format2"
    logger = format2.new(args)
end #endif


As long as each format conforms to the same interface, i.e. it has some sort of method that you can call throughout the program (something like write, log, etc), you should be good to go.
User is offlineProfile CardPM
+Quote Post

tuntis

RE: Loading Class From Folder Based On User Input

8 Oct, 2009 - 09:18 AM
Post #3

New D.I.C Head
*

Joined: 10 Nov, 2008
Posts: 22



Thanked: 1 times
My Contributions
I did obviously think of doing that, but it occurred to me that somebody might want to write their own log/output format, and that they could simply put it into the right directory and it would be usable without any other source hacks.
User is offlineProfile CardPM
+Quote Post

xclite

RE: Loading Class From Folder Based On User Input

8 Oct, 2009 - 10:04 AM
Post #4

LIKE A BOSS
****

Joined: 12 May, 2009
Posts: 655



Thanked: 14 times
My Contributions
I mean I don't see any issue with using the command line argument, and then a
require "#{ARGV[0])" and maybe having a default value if they don't specify one.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 01:56PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month