is_class from a String?

  • Thread starter leandro nascimento camargo
  • Start date
L

leandro nascimento camargo

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).
 
C

Chris Carter

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:
=> true
 
L

leandro nascimento camargo

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

leandro nascimento camargo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top