From: Stu Date: 2011-04-24T17:58:57+09:00 Subject: Re: who can eplain deeply the ruby's methods? To see what methods are associated with what class which is not inherited use this syntax: object.public_methods(false) examples: >> BasicObject.public_methods(false) => [:allocate, :new, :superclass, :to_yaml] >> class MyClass; def hello; "world"; end; end >> x = MyClass.new => # ruby-1.9.2-head :085 > x.public_method(false) >> x.public_methods(false) => [:hello] >> {}.public_methods(false) => [:rehash, :to_hash, :to_a, :inspect, :to_s, :==, :[], :hash, :eql?, :fetch, :[]=, :store, :default, :default=, :default_proc, :default_proc=, :key, :index, :size, :length, :empty?, :each_value, :each_key, :each_pair, :each, :keys, :values, :values_at, :shift, :delete, :delete_if, :keep_if, :select, :select!, :reject, :reject!, :clear, :invert, :update, :replace, :merge!, :merge, :assoc, :rassoc, :flatten, :include?, :member?, :has_key?, :has_value?, :key?, :value?, :compare_by_identity, :compare_by_identity?, :taguri=, :taguri, :yaml_initialize, :to_yaml] and so one. On Sun, Apr 24, 2011 at 3:46 AM, Stu wrote: > 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/. >> >