Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a constant is defined by its symbol in Ruby?

Tags:

ruby

const = :FOO
FOO = :ok
defined? FOO => 'constant'

How to check if FOO is defined using const?

defined? eval( const.to_s )

does not work.

like image 886
B Seven Avatar asked Jul 07 '15 01:07

B Seven


1 Answers

Use const_defined? instead: http://ruby-doc.org/core/classes/Module.html#M000487

Object.const_defined?(const.to_s)
like image 150
Collin Graves Avatar answered Oct 15 '22 17:10

Collin Graves