Forum: Ruby Is there a better way of doing this

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Damjan R. (Guest)
on 2009-04-24 01:18
I have three classes.
module mymodule

class Abstract
end

class A < Abstract
end

class B  < Abstract
end

end

And I want the third class to subclass class A or B dependent on
parameter passed. my_src is also the name of source file:

This is what I do:

I create my_src.rb where I write:
class my_src < Abstract
....
end

Finally I have a method to load my class and do something with it.
def run_class(filename, parm)
  src = File.read(filename + '.rb')
  src.sub!('Abstract', parm)
# Evaluate is equal to load
  eval src.to_s
  obj = eval( File.basename(filename).capitalize + '.new' )
  obj.do_something
end
run_class('my_src')

Ruby is wonderful language which allows us to do this. But the
substitution part is kinda ugly to me.

So is there a better way of doing this?



by
TheR
Robert H. (Guest)
on 2009-04-24 01:36
Would Object.const_get not work?

eval indeed looks ugly.

It kinda reminds me of a farmer who uses a new machine to work on his
field, and then using his bare hands to dig holes for new seeds, because
his new machine looks a bit awkward to him right now, and digging holes
with his hands (using eval) is so much easier...
Damjan R. (Guest)
on 2009-04-24 01:44
Marc H. wrote:
> Would Object.const_get not work?
>
> eval indeed looks ugly.
>
> It kinda reminds me of a farmer who uses a new machine to work on his
> field, and then using his bare hands to dig holes for new seeds, because
> his new machine looks a bit awkward to him right now, and digging holes
> with his hands (using eval) is so much easier...

To be frank this is the solution that I came to. It is ugly but it
works. I don't know (yet) any other way.


by
TheR
Damjan R. (Guest)
on 2009-04-24 01:49
Marc H. wrote:
> Would Object.const_get not work?
>
> eval indeed looks ugly.
>
> It kinda reminds me of a farmer who uses a new machine to work on his
> field, and then using his bare hands to dig holes for new seeds, because
> his new machine looks a bit awkward to him right now, and digging holes
> with his hands (using eval) is so much easier...

And nope:

NameError: wrong constant name My_src.new


by
TheR
Joel VanderWerf (Guest)
on 2009-04-24 02:08
(Received via mailing list)
Damjan R. wrote:
> end
> ....
> end
> run_class('my_src')
>
> Ruby is wonderful language which allows us to do this. But the
> substitution part is kinda ugly to me.
>
> So is there a better way of doing this?

One idea: define A and B as modules instead of classes. Then, when you
load the file that defines "class MySrc < Abstract", you don't have to
substitute anything. Just include the module, as in:

   Module ModA; ...; end

   load filename + ".rb"
   cl = Object.const_get( File.basename(filename).capitalize )
   cl.class_eval {include ModA}

Now, instances of cl will have all the methods of module ModA.
David M. (Guest)
on 2009-04-24 02:44
(Received via mailing list)
On Thursday 23 April 2009 16:18:57 Damjan R. wrote:
> And I want the third class to subclass class A or B dependent on
> parameter passed.

On loading the file? That might not be the best way...

> Finally I have a method to load my class and do something with it.
> def run_class(filename, parm)
>   src = File.read(filename + '.rb')
>   src.sub!('Abstract', parm)
> # Evaluate is equal to load
>   eval src.to_s
>   obj = eval( File.basename(filename).capitalize + '.new' )
>   obj.do_something
> end
> run_class('my_src')

Ok...  First of all, that method is too long... you probably don't want
obj.do_something in there. You probably want it to return obj so you can
do
something with it.

Second: Do A and B have to be classes? Can they be modules instead?
How about Myclass? Can it be a module instead of a class?

Let's say A and B are modules:

class MyClass < Abstract
  ...
end
def run_class type
  obj = MyClass.new
  obj.extend type
  obj
end
run_class(A)
run_class(B)

Or, let's say MyClass is actually MyModule:

class A < Abstract
  include MyModule
end
class B < Abstract
  include MyModule
end
# not sure why you still need this, but here you go:
def run_class type
  type.new
end

Or, if you don't know which classes MyModule might be mixed into, you
could do
something like this:

def run_class type
  obj = type.new
  obj.extend MyModule
end

Or, you could even do hacks like this:

[A, B].each do |type|
  type.send :include, MyModule
end

Which of these routes makes sense depends very much on what you're
trying to
build. I don't know what that is. For example, several people have
suggested
using const_get, but I don't see why you can't just pass the
module/class
itself as a parameter -- they're objects, too!
Tim H. (Guest)
on 2009-04-24 02:53
(Received via mailing list)
Damjan R. wrote:
> end
> ....
> end
> TheR
I'm not sure if this is what you're looking for, but...keep in mind that
  the class name ("Abstract") is just a constant that contains the class
object.  You can assign that class object to a variable and use the
variable in place of the actual class name when you're defining the
subclass. So you could have

class A < Abstract
end

class B < Abstract
end

if use_A then
   sup = A
else
   sup = B
end

class C < sup
end

Furthermore, you can create a new class with Class.new and specify the
superclass as the argument to new. Then you can create new objects of
the class by sending the new method to the class object.

my_cls = Class.new(sup)
my_obj = my_cls.__send__(:new)
Andrew T. (Guest)
on 2009-04-24 07:02
(Received via mailing list)
On Fri, Apr 24, 2009 at 12:51 AM, Tim H. <removed_email_address@domain.invalid>
wrote:
>>
>> I create my_src.rb where I write:
>>  obj = eval( File.basename(filename).capitalize + '.new' )
>>
> end
> class C < sup
> RMagick: http://rmagick.rubyforge.org/
>
>

def run_class(param)
  Class.new(param) do
    def test
      'Hello World'
    end
  end.new #<- Note I've got .new in here
end

c = run_class(A)
c.is_a?(A)
#=> true
c.test
#=> Hello World

Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
Robert K. (Guest)
on 2009-04-24 10:25
(Received via mailing list)
On 24.04.2009 05:01, Andrew T. wrote:
>>>
>>> I create my_src.rb where I write:
>>>  obj = eval( File.basename(filename).capitalize + '.new' )
>>>  obj.do_something
>>> end
>>> run_class('my_src')
>>>
>>> Ruby is wonderful language which allows us to do this. But the
>>> substitution part is kinda ugly to me.
>>>
>>> So is there a better way of doing this?

The question is: of doing what?  You say you want the third class to
subclass either A or B.  But the code you presented just replaces the
first occurrence of "Abstract" with something else.  Also, your
run_class is not called with the second parameter.

If you want to control inheritance from either A or B you can do

class X < condition ? A : B
end

But frankly, this approach seems strange at least.  The question is, why
do you need make the inheritance structure of your class configurable?
The suggestion to make A and B mixin modules seems much better to me.
You can even extend instances of your class with a module on a case by
case basis.

Can you present a bigger picture so we can understand what motivates
your approach?

Kind regards

  robert
Damjan R. (Guest)
on 2009-04-24 11:25
Robert K. wrote:
> On 24.04.2009 05:01, Andrew T. wrote:
>>>>
>>>> I create my_src.rb where I write:
>>>>  obj = eval( File.basename(filename).capitalize + '.new' )
>>>>  obj.do_something
>>>> end
>>>> run_class('my_src')
>>>>
>>>> Ruby is wonderful language which allows us to do this. But the
>>>> substitution part is kinda ugly to me.
>>>>
>>>> So is there a better way of doing this?
>
> The question is: of doing what?  You say you want the third class to
> subclass either A or B.  But the code you presented just replaces the
> first occurrence of "Abstract" with something else.  Also, your
> run_class is not called with the second parameter.
>
> If you want to control inheritance from either A or B you can do
>
> class X < condition ? A : B
> end
>
> But frankly, this approach seems strange at least.  The question is, why
> do you need make the inheritance structure of your class configurable?
> The suggestion to make A and B mixin modules seems much better to me.
> You can even extend instances of your class with a module on a case by
> case basis.
>
> Can you present a bigger picture so we can understand what motivates
> your approach?
>
> Kind regards
>
>   robert

What I am trying to do is create a reporting sistem, something like
Ruport, but simplier and different. So my Class A is ReportPDF and Class
B is ReportHTML and Abstract is ReportBase which defines methods and
variables common to all subclasses. And there can be ReportXX Class in
the future which should expand reporting system to XX format when
required.

What I have accomplished so far is I can call:

run_report('myReportFile', :format => 'HTML')
run_report('myReportFile', :format => 'PDF')

and get result in pdf or html file, with no changes to myReportFile.rb.
Final idea is to make it Rails plugin and run it maybe like this:

  respond_to do |format|
    format.htmlp { render :file => 'myReportFile', format => 'HTML' }
    format.pdf   { render :file => 'myReportFile', format => 'PDF' }
  end

I guess I still have to learn a lot until then.

But it looks like ugly hack and I don't consider myself to be Ruby
hacker. I am just using what I have learned so far and mix it with my
previuos experiances. Which is mostly wrong because I am always thinking
on how I used to do this before Ruby.

So I am asking if this can be accomplished differently?


by
TheR
Andrew T. (Guest)
on 2009-04-24 11:36
(Received via mailing list)
>
For starters, have a look at how Ruport does it.
Also, Gregory actually covers this in his book, Ruby Best Practices
which is worth buying and reading anyway

Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
Robert K. (Guest)
on 2009-04-24 11:44
(Received via mailing list)
2009/4/24 Damjan R. <removed_email_address@domain.invalid>:
>>>>> substitution part is kinda ugly to me.
>> class X < condition ? A : B
>
> run_report('myReportFile', :format => 'PDF')
>
> But it looks like ugly hack and I don't consider myself to be Ruby
> hacker. I am just using what I have learned so far and mix it with my
> previuos experiances. Which is mostly wrong because I am always thinking
> on how I used to do this before Ruby.
>
> So I am asking if this can be accomplished differently?

Yes.  This is a textbook example for using delegation over
inheritance. You should make your Report class *use* a format instead
of *inheriting* a format.  You can still have a formatter base class
which implements common things (e.g. if you want your numbers to come
out identical in all reports then you could have a number formatting
method there).  But the different formatting (e.g. using <li> vs. some
PDF markup) is done in the specific formatter class.

It seems to me that inheritance is generally overused and people
should more often resort to delegation.  Maybe we should blog about
this - although it is not generally a Ruby best practice to delegate
instead of inherit.

Kind regards

robert
Damjan R. (Guest)
on 2009-04-24 12:10
Robert K. wrote:

> It seems to me that inheritance is generally overused and people
> should more often resort to delegation.  Maybe we should blog about
> this - although it is not generally a Ruby best practice to delegate
> instead of inherit.
>
> Kind regards
>
> robert

Can you appoint me to a link, where I could read more about delegation
and how it is done.

by
TheR
Robert D. (Guest)
on 2009-04-24 12:18
(Received via mailing list)
On Fri, Apr 24, 2009 at 10:10 AM, Damjan R. 
<removed_email_address@domain.invalid> wrote:
>
> Can you appoint me to a link, where I could read more about delegation
> and how it is done.

Normally I use Wikipedia as a starting point
http://en.wikipedia.org/wiki/Delegation_(programming)
and often the external links provide some useful information.

HTH
R.
Damjan R. (Guest)
on 2009-04-24 14:47
Joel VanderWerf wrote:
> Damjan R. wrote:
>> end
>> ....
>> end
>> run_class('my_src')
>>
>> Ruby is wonderful language which allows us to do this. But the
>> substitution part is kinda ugly to me.
>>
>> So is there a better way of doing this?
>
> One idea: define A and B as modules instead of classes. Then, when you
> load the file that defines "class MySrc < Abstract", you don't have to
> substitute anything. Just include the module, as in:
>
>    Module ModA; ...; end
>
>    load filename + ".rb"
>    cl = Object.const_get( File.basename(filename).capitalize )
>    cl.class_eval {include ModA}

>
> Now, instances of cl will have all the methods of module ModA.

I am trying this variation for now. And it might look like good until
here:
>    cl.class_eval {include ModA}

I guess my version is wrong
    cl.class_eval { "include 'DrgReports#{opts[:format]}'" }

    report = eval( File.basename(custom_class).capitalize + '.new' )

because after this I get error that method found in included module is
missing.


How to write the line correct?


by
TheR
Robert K. (Guest)
on 2009-04-24 15:14
(Received via mailing list)
2009/4/24 Damjan R. <removed_email_address@domain.invalid>:
>>> So is there a better way of doing this?
>
>>
>> Now, instances of cl will have all the methods of module ModA.
>
> I am trying this variation for now. And it might look like good until
> here:
>>    cl.class_eval {include ModA}
>
> I guess my version is wrong
>    cl.class_eval { "include 'DrgReports#{opts[:format]}'" }

Here you are creating a string only.

>    report = eval( File.basename(custom_class).capitalize + '.new' )
>
> because after this I get error that method found in included module is
> missing.
>
>
> How to write the line correct?

This should work:

cl.class_eval "include 'DrgReports#{opts[:format]}'"

But again, inclusion - which is a special form of inheritance - does
not seem the proper technique here.

Kind regards

robert
Damjan R. (Guest)
on 2009-04-24 15:38
Robert K. wrote:

>> How to write the line correct?
>
> This should work:
>
> cl.class_eval "include 'DrgReports#{opts[:format]}'"
>
> But again, inclusion - which is a special form of inheritance - does
> not seem the proper technique here.
>
> Kind regards
>
> robert

Thanks this works now. I had to leave out single quotes.

def run_report(source_file, opts={}, default_opts={})
  opts[:format] ||= 'HTML'
  load source_file + '.rb'
  cl = Object.const_get File.basename(source_file).capitalize
  cl.class_eval "include DrgReports#{opts[:format]}"
  report = cl.class_eval 'new'

  report.set_options(default_opts)
  report.set_options(opts)
  report.create
  report.do_report
  report.close
end

It looks nicer and I understand a piece of Ruby a little more.

I will try to study delegation this weekend.


by
TheR
This topic is locked and can not be replied to.