How can I choose which version of a module to include dynamically in Ruby? -
I am writing a Ruby command line application which uses fileutils
from the standard library Based on how the invite user invites the app, I include the FileUtils
, FileUtils :: DryRun
or FileUtils :: Verbose
Would like to
Since is included
is private, however, I can not put arguments to choose the object in the initialize
method (this was my first thought, then I could simply pass the information about the user's choice as a parameter of the new
.) I have come up with two options that start working, but I either:
-
Set a global variable in the name of the app on the basis of user's choice, and then Las including conditional in include:
class Worker Case app :: option "File Utils :: Drain etc. in dry runs".
-
Create sub-classes, where the only difference is that the version of
FileUtils
contains the appropriate one on the basis of user choice Select.FileUtils in class worker # End class worker in shared workarms :: Dryn & lt; FileUtils :: DryRun and Class worker in Worker :: Verbose & lt; FileUtils :: Verbose End
The first method seems to be drill-er in the worker, but I hope there is something more simple than this that I have not thought to be. / P>
So what if it's private?
class worker def if verbose (class
special; self; includes FileUtils :: Verbose; end) and (class FileUtils :: something worker
'Metaclass - main code in "test" and end / Pre>Various workers can use different
FileUtils
in this way, not in thecategory.
Comments
Post a Comment