Create a gist now

Instantly share code, notes, and snippets.

@gglin /.pryrc.rb
Last active Dec 19, 2015

What would you like to do?
My .pryrc file
class Object
# all methods named 'local_(*)_methods'
# should return '(*)_methods' minus those that exist in Object
def method_missing(method_name, klass = Object, *args, &block)
if method_name.to_s[0..5] == "local_" && method_name.to_s[-7..-1] == "methods" && !method_name.to_s[6..-1].include?("local")
subname = method_name.to_s[6..-1]
self.send(subname, *args, &block) - klass.send(subname, *args, &block)
else
super(method_name, *args, &block)
end
end
# core 2.0 documentation says "DO NOT USE THIS DIRECTLY." - why?
def respond_to_missing?(method_name, include_private = false)
subname = method_name.to_s[6..-1]
method_name.to_s[0..5] == "local_" && method_name.to_s[-7..-1] == "methods" && !method_name.to_s[6..-1].include?("local") && respond_to?(subname) or super
end
end
class Module
def class_methods(*args, &block)
self.methods(*args, &block) - self.instance_methods(*args, &block)
end
def class_ancestors
self.ancestors.select{|a| a.class == Class}
end
def module_ancestors
self.ancestors.select{|a| a.class == Module}
end
def subthings(recursive = false, *args, &block)
result = self.constants.collect {|const_name| const_get(const_name)}.select {|const| yield(const) }
return [] if result.include?(self)
if recursive == false
result.uniq
else
looper = result.clone
looper.each do |thing|
thing.subthings(false, *args, &block).each do |subthing|
subthing.subthings(false, *args, &block).each do |subsubthing|
recursive = false if result.include?(subsubthing)
end
result << thing.subthings(recursive, *args, &block)
end
end
result.flatten.uniq
end
end
# http://www.natontesting.com/2010/06/30/how-to-get-the-submodules-of-a-ruby-module/
def submodules(recursive = false)
subthings(recursive) {|const| const.class == Module}
end
def subclasses(recursive = false)
subthings(recursive) {|const| const.class == Class}
end
def subconstructs(recursive = false)
subthings(recursive) {|const| const.class == Class || const.class == Module}
end
def subconstruct_modules
subconstructs(true).select {|const| const.class == Module}
end
def subconstruct_classes
subconstructs(true).select {|const| const.class == Class}
end
def subconstants(recursive = false)
subthings(recursive) {|const| const.class != Class && const.class != Module}
end
def subconstant_names
constants.select {|const_name| const_get(const_name).class != Class && const_get(const_name).class != Module}
end
end
class Class
def child_classes
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
def children
child_classes
end
def siblings
self.superclass.child_classes - [self]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment