Skip to content

Instantly share code, notes, and snippets.

@JonRowe
Created March 14, 2013 01:40
Show Gist options
  • Save JonRowe/5158141 to your computer and use it in GitHub Desktop.
Save JonRowe/5158141 to your computer and use it in GitHub Desktop.
Fetch all the classes in the Object namespace...
def blacklist
[
:Object, :Module, :Class, :BasicObject, :Kernel, :NilClass, :NIL, :Data,
:TrueClass, :TRUE, :FalseClass, :FALSE, :Encoding, :Comparable, :Method,
:String, :Symbol, :Exception, :SystemExit, :SignalException, :Interrupt,
:StandardError, :TypeError, :ArgumentError, :IndexError, :KeyError, :Dir,
:RangeError, :ScriptError, :SyntaxError, :LoadError, :NotImplementedError,
:NameError, :NoMethodError, :RuntimeError, :SecurityError, :NoMemoryError,
:EncodingError, :SystemCallError, :Errno, :ZeroDivisionError, :Enumerable,
:FloatDomainError, :Numeric, :Integer, :Fixnum, :Float, :Bignum, :Array,
:Hash, :ENV, :Struct, :RegexpError, :Regexp, :MatchData, :Marshal, :Range,
:IOError, :EOFError, :IO, :STDIN, :STDOUT, :STDERR, :ARGF, :FileTest, :File,
:Time, :Random, :Signal, :Process, :Proc, :LocalJumpError, :UnboundMethod,
:Binding, :Math, :GC, :ObjectSpace, :Enumerator, :StopIteration, :RubyVM,
:Thread, :TOPLEVEL_BINDING, :ThreadGroup, :Mutex, :ThreadError, :Fiber,
:FiberError, :Rational, :Complex, :RUBY_RELEASE_DATE, :RUBY_PLATFORM, :ARGV,
:RUBY_PATCHLEVEL, :RUBY_REVISION, :RUBY_DESCRIPTION, :RUBY_COPYRIGHT, :Gem,
:RUBY_ENGINE, :TracePoint, :RbConfig, :Config, :CROSS_COMPILING, :Date, :IRB,
:Exception2MessageMapper, :RubyToken, :RubyLex, :Readline, :SystemStackError
] + ENV.keys.map(&:to_sym)
end
def constants_for name
(name.constants - blacklist).map { |constant| name.const_get constant }
end
def classes_for constant
if constant.is_a? Class
[constant]
elsif constant.is_a? Module
constants_for(constant).inject([]) do |array,constant|
array << classes_for(constant)
array
end
else
[]
end
end
def constants
constants_for(Object)
end
def classes
constants.map { |const| classes_for const }.flatten
end
module A
class B
end
end
class C
end
puts "Consts => #{constants}"
puts "Classes => #{classes}"
@agerwick
Copy link

Seems that one of my classes are causing an issue here, but this is more than enough to get me going anyway - thanks! :)

irb(main):001:0> def blacklist
irb(main):002:1>   [
irb(main):003:2*     :Object, :Module, :Class, :BasicObject, :Kernel, :NilClass, :NIL, :Data,
irb(main):004:2*     :TrueClass, :TRUE, :FalseClass, :FALSE, :Encoding, :Comparable, :Method,
irb(main):005:2*     :String, :Symbol, :Exception, :SystemExit, :SignalException, :Interrupt,
irb(main):006:2*     :StandardError, :TypeError, :ArgumentError, :IndexError, :KeyError, :Dir,
irb(main):007:2*     :RangeError, :ScriptError, :SyntaxError, :LoadError, :NotImplementedError,
irb(main):008:2*     :NameError, :NoMethodError, :RuntimeError, :SecurityError, :NoMemoryError,
irb(main):009:2*     :EncodingError, :SystemCallError, :Errno, :ZeroDivisionError, :Enumerable,
irb(main):010:2*     :FloatDomainError, :Numeric, :Integer, :Fixnum, :Float, :Bignum, :Array,
irb(main):011:2*     :Hash, :ENV, :Struct, :RegexpError, :Regexp, :MatchData, :Marshal, :Range,
irb(main):012:2*     :IOError, :EOFError, :IO, :STDIN, :STDOUT, :STDERR, :ARGF, :FileTest, :File,
irb(main):013:2*     :Time, :Random, :Signal, :Process, :Proc, :LocalJumpError, :UnboundMethod,
irb(main):014:2*     :Binding, :Math, :GC, :ObjectSpace, :Enumerator, :StopIteration, :RubyVM,
irb(main):015:2*     :Thread, :TOPLEVEL_BINDING, :ThreadGroup, :Mutex, :ThreadError, :Fiber,
irb(main):016:2*     :FiberError, :Rational, :Complex, :RUBY_RELEASE_DATE, :RUBY_PLATFORM, :ARGV,
irb(main):017:2*     :RUBY_PATCHLEVEL, :RUBY_REVISION, :RUBY_DESCRIPTION, :RUBY_COPYRIGHT, :Gem,
irb(main):018:2*     :RUBY_ENGINE, :TracePoint, :RbConfig, :Config, :CROSS_COMPILING, :Date, :IRB,
irb(main):019:2*     :Exception2MessageMapper, :RubyToken, :RubyLex, :Readline, :SystemStackError
irb(main):020:2>   ] + ENV.keys.map(&:to_sym)
irb(main):021:1> end
=> nil

irb(main):022:0>  
irb(main):023:0* def constants_for name
irb(main):024:1>   (name.constants - blacklist).map { |constant| name.const_get constant }
irb(main):025:1> end
=> nil

irb(main):026:0>  
irb(main):027:0* def classes_for constant
irb(main):028:1>   if constant.is_a? Class
irb(main):029:2>     [constant]
irb(main):030:2>   elsif constant.is_a? Module
irb(main):031:2>     constants_for(constant).inject([]) do |array,constant|
irb(main):032:3*       array << classes_for(constant)
irb(main):033:3>       array
irb(main):034:3>     end
irb(main):035:2>   else
irb(main):036:2*     []
irb(main):037:2>   end
irb(main):038:1> end
=> nil

irb(main):039:0>  
irb(main):040:0* def constants
irb(main):041:1>   constants_for(Object)
irb(main):042:1> end
=> nil

irb(main):043:0> def classes
irb(main):044:1>   constants.map { |const| classes_for const }.flatten
irb(main):045:1> end
=> nil

irb(main):046:0>  
irb(main):047:0* module A
irb(main):048:1>   class B
irb(main):049:2>   end
irb(main):050:1> end
=> nil
irb(main):051:0> class C
irb(main):052:1> end
=> nil
irb(main):053:0>  
irb(main):054:0* puts "Consts  => #{constants}"
Consts  => ["1.9.2", "/home/leafpile/app/current", ConditionVariable, Queue, SizedQueue, Etc, Syck, Psych, StringIO, DateTime, Syck, Yajl, JSON, Faker, OpenStruct, Shellwords, URI, Rack, ERB, StringScanner, StringScanner::Error, FileUtils, Set, SortedSet, Delegator, SimpleDelegator, Tempfile, Sinatra, Digest, Tilt, Monk, OpenSSL, Fcntl, SecureRandom, MonitorMixin, Monitor, Logger, Base64, BasicSocket, Socket, SocketError, IPSocket, TCPSocket, TCPServer, UDPSocket, UNIXSocket, UNIXServer, Addrinfo, Redis, Timeout, Timeout::Error, Ohm, Haml, CGI, Rtopia, I18n, Pagination, CanCan, Nest, Ost, RSolr, LightConfig, Sunspot, IPAddr, Mail, ActiveSupport, ActiveSupport::HashWithIndifferentAccess, Net, MIME, Singleton, Treetop, IntervalSkipList, Forwardable, SingleForwardable, CSV, Iconv, Spreadsheet, RecursivelyEnumerable, Ole, RangesIO, RangesIONonResizeable, Translatable, CanonicalizeGender, JsFiles, AuditTrail, PhoneNumber, AddressParser, Mac, UUIDGen, UUID, OpenURI, GoogleSearch, SolrIndexing, DuplicationMarking, LazyAssignTagIds, Paranoia, Taggability, TagInheritance, PersonContactDetailsConcerns, PersonSunspotConcerns, HasContactDetails, HasDocuments, GroupByHelpers, HasCommunications, List, BigDecimal, Person, CanonicalizeMaritalStatus, Nokogiri, Racc, Racc::ParseError, ImageGrabber, HasCandidates, CountryCode, FixActions, Assignments, AssignmentSunspotConcerns, Assignment, CanonicalizeAssignmentStatus, EncodingHelpers, BizDevs, BizDevSunspotConcerns, BizDev, CanonicalizeBizDevCandidateStage, OhmInstanceAdapter, OhmDataAccessor, UserAgent, CompanySunspotConcerns, Company, CanonicalizeCompanyStatus, Readability, HasActivityTypes, PdfToText, CanonicalizeBizDevStatus, CanonicalizeCountry, LP, Sequel, LegacyConnect, Repeater, OpenOffice, Main, Benchmark, ContactDetailsHelpers, TagHelpers, CountriesHelpers, IconHelpers, UserAgentHelpers, IfHelpers, IPHelpers, AddressHelpers, Mobile, Biography, TagNote, Education, PersonSearch, ContactDetailPresenter, BizDevCandidateSunspotConcerns, BizDevCandidate, Candidates, CandidateSunspotConcerns, Candidate, Address, Ability, Document, SourceSunspotConcerns, Source, AdvancedSearchQuery, Job, CandidateActivity, UserSunspotConcerns, User, ContactDetail, ContactLogSunspotConcerns, Communication, DuplicationNotice, Notification, Indexer, CompanyInfo, BizDevCandidateActivity, School, SubContactLog, SheetExporter, MergedPerson, Connection, TagSunspotConcerns, Tags, Email, Synonym, MergedCompany, AccessLog, IPRange, MergedPosition, Group, Tag, PositionSunspotConcerns, Comment, Duplicate, Position, DesignTest, A, C]

=> nil

irb(main):055:0> puts "Classes => #{classes}"
LoadError: no such file to load -- fcgi
  from /home/leafpile/app/current/vendor/rack-1.3.0/lib/rack/handler/fastcgi.rb:1:in `require'
  from /home/leafpile/app/current/vendor/rack-1.3.0/lib/rack/handler/fastcgi.rb:1:in `<top (required)>'
  from (irb):24:in `const_get'
  from (irb):24:in `block in constants_for'
  from (irb):24:in `map'
  from (irb):24:in `constants_for'
  from (irb):31:in `classes_for'
  from (irb):32:in `block in classes_for'
  from (irb):31:in `each'
  from (irb):31:in `inject'
  from (irb):31:in `classes_for'
  from (irb):44:in `block in classes'
  from (irb):44:in `map'
  from (irb):44:in `classes'
  from (irb):55
  from /usr/bin/irb:12:in `<main>'
irb(main):056:0> 

@JonRowe
Copy link
Author

JonRowe commented Mar 14, 2013

It also might be because I setup the blacklist based on 2.0.0 constants, just add the extra 1.9.2 ones into the black list...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment