Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/sixarm_ruby_string_to_class.rb

Instance Method Summary (collapse)

Instance Method Details

- (Class) to_class

Ruby String#to_class method to convert from a String to a class

From Mirage at infovore.org/archives/2006/08/02/getting-a-class-object-in-ruby-from-a-string-containing-that-classes-name/

Examples:

"Foo::Bar".to_class => Foo::Bar

Returns:

  • (Class)

    the string converted to a class



18
19
20
# File 'lib/sixarm_ruby_string_to_class.rb', line 18

def to_class
  split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
end