Created
November 25, 2009 17:39
-
-
Save patrickt/242894 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ObjectSpace | |
def each_instance_of(klass) | |
each_object do |obj| | |
yield(obj) if obj.instance_of? klass | |
end | |
end | |
def each_kind_of(klass) | |
each_object do |obj| | |
yield(obj) if obj.kind_of? klass | |
end | |
end | |
def each_such_that(query) | |
each_object do |obj| | |
yield(obj) if query[obj] | |
end | |
end | |
end | |
include ObjectSpace | |
class Foo; def whatever; end; end | |
x, y, z = Foo.new, Foo.new, Foo.new | |
class Bar; end | |
a = Bar.new | |
each_instance_of(Foo) { |x| p x } | |
each_such_that(->(x) { x.respond_to? :whatever }) { p x } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment