[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

is_class from a String?

leandro camargo

2/9/2007 12:22:00 AM

I have a dynamic String entry and I want know if it represents some
any existing Class name (in a given modules set loaded)?

How to do that.
PS.: I'm using Rails and I need to know if there is some Model with a
given name (or symbol).

3 Answers

Chris Carter

2/9/2007 12:48:00 AM

0

On 2/8/07, leandro nascimento camargo <leandroico@gmail.com> wrote:
> I have a dynamic String entry and I want know if it represents some
> any existing Class name (in a given modules set loaded)?
>
> How to do that.
> PS.: I'm using Rails and I need to know if there is some Model with a
> given name (or symbol).
>
>
>
Hi Leandro:
>> class Model
>> end
=> nil
>> Module.const_get("Model").is_a? Class
=> true

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

leandro camargo

2/9/2007 1:07:00 AM

0

Hi Chris...
But If Model doesn't exist I will get an error. =/
You see know the problem?
But maybe I can use const_defined before. =]
I will test here.
Thank you, anyway.

> Hi Leandro:
> >> class Model
> >> end
> => nil
> >> Module.const_get("Model").is_a? Class
>
> => true
>
> --
> Chris Carter
> concentrationstudios.com
> brynmawrcs.com


leandro camargo

2/9/2007 1:28:00 AM

0

Yay!
Solved!
I simply used an exception treatment like this:

is_model =
begin
ActiveRecord.const_get(c.name[0...-3].camelize).is_a? Class
rescue StandardError => e
false
end