Module: CodyRobbins::ToClass

Defined in:
lib/cody_robbins/to_class.rb

Instance Method Summary collapse

Instance Method Details

#to_classClass

Converts the object in question to Camel case and then tries to find a declared constant with the corresponding name. This combines the functionality of ActiveSupport’s camelize and constantize methods, and allows you to easily get a class from its name with one simple method invokation.

Examples:

:user.to_class       #=> User
'user'.to_class      #=> User
:blog_post.to_class  #=> BlogPost
'blog_post'.to_class #=> BlogPost

Returns:

  • (Class)

    The class corresponding to the representation of its name.



14
15
16
# File 'lib/cody_robbins/to_class.rb', line 14

def to_class
  to_s.camelize.constantize
end