Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 377643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:43:00+00:00 2026-05-12T14:43:00+00:00

Taken from a previous post with some modifications to respond to sepp2k ‘s comment

  • 0

Taken from a previous post with some modifications to respond to sepp2k‘s comment about namespaces, I have implemented String#to_class method. I’m sharing the code here and I do believe that it could be refactored someway specially the “i” counter. Your comments are appreciated.

 class String
   def to_class
     chain = self.split "::"
     i=0
     res = chain.inject(Module) do |ans,obj|
       break if ans.nil?
       i+=1
       klass = ans.const_get(obj)
       # Make sure the current obj is a valid class 
       # Or it's a module but not the last element, 
       # as the last element should be a class
       klass.is_a?(Class) || (klass.is_a?(Module) and i != chain.length) ? klass : nil
     end
   rescue NameError
     nil
   end
 end

 #Tests that should be passed.
 assert_equal(Fixnum,"Fixnum".to_class)
 assert_equal(M::C,"M::C".to_class)
 assert_nil "Math".to_class
 assert_nil "Math::PI".to_class
 assert_nil "Something".to_class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:43:00+00:00Added an answer on May 12, 2026 at 2:43 pm

    I ran some benchmarks by curiosity and my solution is very slow!
    here is a refactored solution with benchmarks, hope that helps.

    require "benchmark"
    
    class String
      def to_class_recursive
        chain = self.split "::"
        klass = parent.const_get chain.shift
        return chain.size < 1 ? (klass.is_a?(Class) ? klass : nil) : chain.join("::").to_class(klass)
      rescue
        nil
      end
    
      def to_class_original
        chain = self.split "::"
        i=0
        res = chain.inject(Module) do |ans,obj|
          break if ans.nil?
          i+=1
          klass = ans.const_get(obj)
          # Make sure the current obj is a valid class 
          # Or it's a module but not the last element, 
          # as the last element should be a class
          klass.is_a?(Class) || (klass.is_a?(Module) and i != chain.length) ? klass : nil
        end
      rescue NameError
        nil
      end
    
      def to_class_refactored
        chain = self.split "::"
        klass = Kernel
        chain.each do |klass_string|
          klass = klass.const_get klass_string
        end
        klass.is_a?(Class) ? klass : nil
      rescue NameError
        nil
      end
    end
    
    module M
      class C
      end
    end
    
    n = 100000
    class_string = "M::C"
    Benchmark.bm(20) do |x|
      x.report("to_class_recursive") { n.times { class_string.to_class_recursive } }
      x.report("to_class_original") { n.times { class_string.to_class_original } }
      x.report("to_class_refactored") { n.times { class_string.to_class_refactored } }
    end
    
    #                           user     system      total        real
    # to_class_recursive    2.430000   0.170000   2.600000 (  2.701991)
    # to_class_original     1.000000   0.010000   1.010000 (  1.049478)
    # to_class_refactored   0.570000   0.000000   0.570000 (  0.587346)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code examples from some of my previous work that help me to
I'd like to build a query string based on values taken from 5 groups
I am using some code which was originally taken from the Apple sample ViewTransitions
Let's say I have N pictures of an object, taken from N know positions.
I have taken an image from UIImagePickerController , but the file looks too big
I am using asp.net mvc for an application. I've taken some guidance from Rob
Can the performance of this sequential search algorithm (taken from The Practice of Programming
I want to be able to compare an image taken from a webcam to
I've taken over a mixed PHP4/PHP5 project which has been handed down from developer
I've taken the plunge and upgraded (or maybe downgraded?!) from WinXP to Vista. Everything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.