Skip to content

Instantly share code, notes, and snippets.

@patrickt
Created November 25, 2009 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickt/242894 to your computer and use it in GitHub Desktop.
Save patrickt/242894 to your computer and use it in GitHub Desktop.
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