Search for all classes whose name matches the string

I would like to find all ruby ​​classes whose name matches the string (ignoring the case). This means that the string must be a substring of the class name.
Therefore, when you look for "stri", you should get the String class (among others).

Do you know a convenient and not too inefficient way to do this?

+2
source share
1 answer
 ObjectSpace.each_object(Class) .select{|klass| klass.name.downcase.include?("stri".downcase)} # => [ String, RubyToken::TkDXSTRING, RubyToken::TkDSTRING, RubyToken::TkXSTRING, RubyToken::TkSTRING ] 
+5
source

Source: https://habr.com/ru/post/1480518/


All Articles