PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Dynamically call classes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Dynamically call classes

Réponse
 
LinkBack Outils de la discussion
Vieux 20/11/2007, 09h59   #1
Tizian Taz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Dynamically call classes

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/.

  Réponse avec citation
Vieux 20/11/2007, 10h05   #2
Peter Szinek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically call classes

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

  Réponse avec citation
Vieux 20/11/2007, 10h21   #3
Tizian Taz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically call classes

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/.

  Réponse avec citation
Vieux 20/11/2007, 10h41   #4
Tizian Taz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically call classes

Don't care about what I said, it works fine!

Thank you very much!

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

  Réponse avec citation
Vieux 20/11/2007, 10h51   #5
Peter Szinek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically call classes

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


  Réponse avec citation
Vieux 20/11/2007, 13h37   #6
Rick DeNatale
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically call classes

On Nov 20, 2007 5:21 AM, Tizian Taz <tazmaniack@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 ing. 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/

  Réponse avec citation
Vieux 21/11/2007, 23h02   #7
jacob.dunphy@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically call classes

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 ing. 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/


  Réponse avec citation
Vieux 22/11/2007, 00h20   #8
ara.t.howard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically call classes


On Nov 21, 2007, at 4:02 PM, jacob.dunphy@gmail.com 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




  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h40.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15880 seconds with 16 queries