Forum: Ruby Using a string to find a class

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.
Pat M. (Guest)
on 2006-06-08 06:28
(Received via mailing list)
I've got a string, and I want to find out if there's a class with that
name.  So something like

class_exists? "Foo"

and then I can eval it if it does, I guess.  Or is there a better way
than eval in that case?  At any rate, I just want to be able to take a
string and find out if there's a class with that name or not.

Thanks,
Pat
Daniel -. (Guest)
on 2006-06-08 06:41
(Received via mailing list)
Hi Pat

This ones come up a few times on the list

http://www.ruby-forum.com/topic/66482#80672

is one that I remember particulalry.

Although in some methods that I have seen in the rails source there has
been

string_name.camelize.constantize

This will return a class object specified by name.
eg

def do_somthing( class_name )
  obj = class_name.to_s.camelize.constantize
  obj.new
end

Hope this works for you
Dave B. (Guest)
on 2006-06-08 07:02
(Received via mailing list)
Pat M. wrote:
> I've got a string, and I want to find out if there's a class with that
> name.  So something like
>
> class_exists? "Foo"
>
> and then I can eval it if it does, I guess.  Or is there a better way
> than eval in that case?  At any rate, I just want to be able to take a
> string and find out if there's a class with that name or not.

module.const_get "Foo"
# or just
const_get "Foo"

Cheers,
Dave
James B. (Guest)
on 2006-06-08 07:20
(Received via mailing list)
Dave B. wrote:
>
>
> module.const_get "Foo"
> # or just
> const_get "Foo"

You'll get an exception if Foo isn't defined.

And not every constant refers to a class you can instantiate:

   class Bar ; end
   kls = Object.const_get "Bar"
   c  = kls.new() # OK

   Foo = 12
   kls = Object.const_get "Foo"
   c  = kls.new() # Not so OK


Quick version:

  def class_exists? kls
   begin
     kls = Object.const_get kls
     kls.kind_of? Class
   rescue Exception
     false
   end
  end


  def new_if_class kls, default = nil
   class_exists?( kls ) ?  Object.const_get( kls ).new : default
  end


  b = new_if_class "Bar"
  p b

  c = new_if_class "Foo"
  p c

--
James B.

"Take eloquence and wring its neck."
  - Paul Verlaine
unknown (Guest)
on 2006-06-08 16:09
(Received via mailing list)
Use Facets' kernel/constant

   constant(name)

or string/to_const

  name.to_const

http:://facets.rubyforge.org

T.
Gregory S. (Guest)
on 2006-06-08 17:43
(Received via mailing list)
On Thu, Jun 08, 2006 at 11:38:42AM +0900, Daniel N wrote:
[...]
} Although in some methods that I have seen in the rails source there
has been
}
} string_name.camelize.constantize
}
} This will return a class object specified by name.
} eg
}
} def do_somthing( class_name )
}  obj = class_name.to_s.camelize.constantize
}  obj.new
} end

Both #camelize and #constantize are Rails extensions.

} Hope this works for you
--Greg

} On 6/8/06, Pat M. <removed_email_address@domain.invalid> wrote:
} >I've got a string, and I want to find out if there's a class with
that
} >name.  So something like
} >
} >class_exists? "Foo"
} >
} >and then I can eval it if it does, I guess.  Or is there a better way
} >than eval in that case?  At any rate, I just want to be able to take
a
} >string and find out if there's a class with that name or not.
} >
} >Thanks,
} >Pat
} >
} >
}
}
Gregory S. (Guest)
on 2006-06-08 17:43
(Received via mailing list)
On Thu, Jun 08, 2006 at 11:26:21AM +0900, Pat M. wrote:
} I've got a string, and I want to find out if there's a class with that
} name.  So something like
}
} class_exists? "Foo"
}
} and then I can eval it if it does, I guess.  Or is there a better way
} than eval in that case?  At any rate, I just want to be able to take a
} string and find out if there's a class with that name or not.

http://redcorundum.blogspot.com/2006/05/kernelqual...

} Thanks,
} Pat
--Greg
This topic is locked and can not be replied to.