Listing Methods Unique to an Object

Problem

When you list the methods available to an object, the list is cluttered with extraneous methods defined in the object's superclasses and mixed-in modules. You want to see a list of only the methods defined by that object's direct class.

Solution

Subtract the instance methods defined by the object's superclass. You'll be left with only the methods defined by the object's direct class (plus any methods defined on the object after its creation). The my_methods_only method defined below gives this capability to every Ruby object:

	class Object
	 def 
my_methods_only
	 my_super = self.class.superclass
	 return my_super ? methods - my_super.instance_methods : methods
	 end
	end

	s = ''
	s.methods.size # => 143
	Object.instance_methods.size # => 41
	s.my_methods_only.size # => 102
	(s.methods - Object.instance_methods).size # => 102

	def s.singleton_method( )
	end
	s.methods.size # => 144
	s.my_methods_only.size # => 103

	class Object
	 def new_object_method
	 end
	end
	s.methods.size # => 145
	s.my_methods_only.size # => 103

	class MyString < String
	 def my_string_method
	 end
	end
	MyString.new.my_methods_only # => ["my_string_method"]

 

Discussion

The my_methods_only technique removes methods defined in the superclass, the parent classes of the superclass, and in any mixin modules included by those classes. For instance, it removes the 40 methods defined by the Object class when it mixed in the Kernel module. It will not remove methods defined by mixin modules included by the class itself.

Usually these methods aren't clutter, but there can be a lot of them (for instance, Enumerable defines 22 methods). To remove them, you can start out with my_methods_only, then iterate over the ancestors of the class in question and subtract out all the methods defined in modules:

	class Object
	 def my_methods_only_no_mixins
	 self.class.ancestors.inject(methods) do |mlist, ancestor|
	 mlist = mlist - ancestor.instance_methods unless ancestor.is_a? Class
	 mlist
	 end
	end

	[].methods.size # => 121
	[].my_methods_only.size # => 78
	[].my_methods_only_no_mixins.size # => 57

 

See Also

  • Recipe 10.1, "Finding an Object's Class and Superclass," explores ancestors in more detail


Strings

Numbers

Date and Time

Arrays

Hashes

Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming

XML and HTML

Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration



Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net