Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Dynamically call classes

Reply
Thread Tools

Dynamically call classes

 
 
Tizian Taz
Guest
Posts: n/a
 
      11-20-2007
Hello,

I'm new to Ruby but have got several years of experience with PHP. I
work on a project with several classes witch analyse text blocks. The
blocks begin with headers like "Status" or "Update" but that's not my
problem.

I'd like to call classes dynamically like this :

var = 'Status'
# and now I want to call the class "Status"

How can I manage it?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Peter Szinek
Guest
Posts: n/a
 
      11-20-2007
Tizian Taz wrote:
> Hello,
>
> I'm new to Ruby but have got several years of experience with PHP. I
> work on a project with several classes witch analyse text blocks. The
> blocks begin with headers like "Status" or "Update" but that's not my
> problem.
>
> I'd like to call classes dynamically like this :
>
> var = 'Status'
> # and now I want to call the class "Status"
>
> How can I manage it?


>> Kernel.const_get('Status')

=> Status

cheers,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org

 
Reply With Quote
 
 
 
 
Tizian Taz
Guest
Posts: n/a
 
      11-20-2007
Ok, but now, how can I access for example the method Hello which this
existing class inheritated from his mother-class?

I ask this because "Status.Hello" doesn't work...

Maybe I wasn't clear enough in my description :

The classes that I want to call all exist but I don't want to make a
huge amount of tests to know which I have to call. Tell me if I'm not
clear :S

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Tizian Taz
Guest
Posts: n/a
 
      11-20-2007
Don't care about what I said, it works fine!

Thank you very much!

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Peter Szinek
Guest
Posts: n/a
 
      11-20-2007
Tizian Taz wrote:
> Ok, but now, how can I access for example the method Hello which this
> existing class inheritated from his mother-class?
>
> I ask this because "Status.Hello" doesn't work...
>
> Maybe I wasn't clear enough in my description :
>
> The classes that I want to call all exist but I don't want to make a
> huge amount of tests to know which I have to call. Tell me if I'm not
> clear :S
>


class Animal
def ooze
puts "oooooo"
end

def Animal.winkle
puts "winkle"
end
end

class Dog < Animal
end

Kernel.const_get("Dog").new.ooze
Kernel.const_get("Dog").winkle


HTH,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org


 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      11-20-2007
On Nov 20, 2007 5:21 AM, Tizian Taz <> wrote:
> Ok, but now, how can I access for example the method Hello which this
> existing class inheritated from his mother-class?
>
> I ask this because "Status.Hello" doesn't work...
>
> Maybe I wasn't clear enough in my description :
>
> The classes that I want to call all exist but I don't want to make a
> huge amount of tests to know which I have to call. Tell me if I'm not
> clear :S


You can do this, and others are already helping. but...

Do you really have a firm requirement to use a string to represent the class.

Why not just:

var = Status

...

var.Hello

Classes in Ruby are objects and variables can be used to refer to them.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

 
Reply With Quote
 
jacob.dunphy@gmail.com
Guest
Posts: n/a
 
      11-21-2007
And with this, you can do the var = Kernel.const_get("ClassName") and
you have a reference to that class for the scope of your operation.

On Nov 20, 5:37 am, "Rick DeNatale" <rick.denat...@gmail.com> wrote:
> On Nov 20, 2007 5:21 AM, Tizian Taz <tazmani...@gmail.com> wrote:
>
> > Ok, but now, how can I access for example the method Hello which this
> > existing class inheritated from his mother-class?

>
> > I ask this because "Status.Hello" doesn't work...

>
> > Maybe I wasn't clear enough in my description :

>
> > The classes that I want to call all exist but I don't want to make a
> > huge amount of tests to know which I have to call. Tell me if I'm not
> > clear :S

>
> You can do this, and others are already helping. but...
>
> Do you really have a firm requirement to use a string to represent the class.
>
> Why not just:
>
> var = Status
>
> ...
>
> var.Hello
>
> Classes in Ruby are objects and variables can be used to refer to them.
>
> --
> Rick DeNatale
>
> My blog on Rubyhttp://talklikeaduck.denhaven2.com/


 
Reply With Quote
 
ara.t.howard
Guest
Posts: n/a
 
      11-22-2007

On Nov 21, 2007, at 4:02 PM, wrote:

> And with this, you can do the var = Kernel.const_get("ClassName") and
> you have a reference to that class for the scope of your operation.


imho const_get is just to fragile for most uses, i prefer something
like this:



cfp:~ > cat a.rb
class Class
def self.for string
value =
Thread.new do
$SAFE = 4
eval string.to_s, TOPLEVEL_BINDING.dup
end.value
raise ArgumentError unless value.is_a? Class
value
end
end

p Class.for('File::Stat')
p Class.for('Foo::Bar')



cfp:~ > ruby a.rb
File::Stat
a.rb:6:in `eval': (eval):1: uninitialized constant Foo (NameError)
from a.rb:4:in `value'
from a.rb:4:in `for'
from a.rb:14



a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes within classes David ASP .Net 2 07-22-2005 07:13 PM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM
Modifiers applied to attributes, local variables, member functions, classes and inncer classes ! Razvan Java 11 07-17-2004 08:57 PM
Can I using reflection to get all child classes or classes undera package dynamically? Carfield Yim Java 1 05-31-2004 05:33 PM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57