From: Stu Date: 2011-04-24T17:46:59+09:00 Subject: Re: who can eplain deeply the ruby's methods? It's an object oriented inheritance hierarchy of abstract data types. For example run this in irb: String.new.class then String.class.ancestors This will give you an array which is the inheritance chain all the way down to BasicObject which for most abstract data types is the root with the exceptions of NilClass, TrueClass, and FalseClass. The function *_methods are to aid in reflective programming and keep track of specific function types to that class( or object for singletons). Grep can be used to search the object path in the case of function overloading one can call super which may open up alternative ways to deal with message passing, signals and polymorphism. The simplest way to "see" this is to start with your base class. In this case BasicObject and see what methods are in there. From there move up the line. Some of the data types break off in a tree like structure but share the common functions as you move up the tree. In when the same functions are needed they are overwritten in a top level class to deal with the data types special need. This can be seen more simply by looking at Float and Integer: >> 3.14.class.ancestors => [Float, Numeric, Comparable, Object, Kernel, BasicObject] >> 42.class.ancestors => [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject] Fixnum is ruby's way of dealing with small numbers( Bignum is it's analogue) but ignore that for now as they are both branch off the same concept of integers. Notice both Float and Integer classes share Numeric and Comparable. Both inherit from the same common set of rules. Then somewhere in each respective top level class they overide those rules as needed to provide common syntax and function they share. In object oriented speak this is polymorphism. Another great example would be Array and Hash. Both share Enumerable and I'm sure both override the .each method to deal with the abstraction of an indexed array is an associative one. In the end they are just containers data structures that are pure abstracted and feature endowed linked lists. This saves us calls to self and dealing with reverse Hungarian notation styled function names since ruby's methods and aliases are well thought out and can be used as a template or standard to follow in your own classes you create with them. Hope that helps you a bit ~Stu On Sun, Apr 24, 2011 at 12:57 AM, savin max wrote: > I wants to know the ruby's methods' describe,the relationship between > them,also the mechanism(most wants to know). > > what are > > methods: > public_methods: > protected_methods: > private_methods: > ----------------------------------------------------------------------------- > #e.g(from ruby API 1.9.2) > > mod.instance_methods(include_super=true) → array >    Returns an array containing the names of the public and protected > instance methods in the receiver. For a module, these are the public and > protected methods; for a class, they are the instance (not singleton) > methods. With no argument, or with an argument that is false, the > instance methods in mod are returned, otherwise the methods in mod and > mod‘s superclasses are returned. > > mod.public_instance_methods(include_super=true) → array > Returns a list of the public instance methods defined in mod. If the > optional parameter is not false, the methods of any ancestors are > included. > > mod.protected_instance_methods(include_super=true) → array > Returns a list of the protected instance methods defined in mod. If the > optional parameter is not false, the methods of any ancestors are > included. > > mod.private_instance_methods(include_super=true) → array > Returns a list of the private instance methods defined in mod. If the > optional parameter is not false, the methods of any ancestors are > included. > > > ----------------------------------------------------------------------------- > > obj.singleton_methods(all=true) → array > Returns an array of the names of singleton methods for obj. If the > optional all parameter is true, the list will include methods in modules > included in obj. Only public and protected singleton methods are > returned. > > > what's the relationship between these methods? > #e.g. >    String.instance_methods == "str".methods #true >    String.methods(false) == String.singleton_methods(false)  #true >    class method is the singleton method? > ..... > ...... > ....... > > I wants to know the ruby's methods' describe,the relationship between > them,also the mechanism(most wants to know). > > who can Explain? > thanks... > > -- > Posted via http://www.ruby-forum.com/. >