Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3420 > unrolled thread

who can eplain deeply the ruby's methods?

Started bysavin max <mafei.198@gmail.com>
First post2011-04-24 00:57 -0500
Last post2011-04-26 05:06 -0500
Articles 9 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  who can eplain deeply the ruby's methods? savin max <mafei.198@gmail.com> - 2011-04-24 00:57 -0500
    Re: who can eplain deeply the ruby's methods? Stu <stu@rubyprogrammer.net> - 2011-04-24 03:46 -0500
      Re: who can eplain deeply the ruby's methods? Stu <stu@rubyprogrammer.net> - 2011-04-24 03:58 -0500
      Re: who can eplain deeply the ruby's methods? savin max <mafei.198@gmail.com> - 2011-04-24 08:18 -0500
        Re: who can eplain deeply the ruby's methods? Stu <stu@rubyprogrammer.net> - 2011-04-24 15:53 -0500
          Re: who can eplain deeply the ruby's methods? savin max <mafei.198@gmail.com> - 2011-04-24 22:10 -0500
    Re: who can eplain deeply the ruby's methods? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-25 19:35 -0500
    Re: who can eplain deeply the ruby's methods? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-25 19:57 -0500
      Re: who can eplain deeply the ruby's methods? savin max <mafei.198@gmail.com> - 2011-04-26 05:06 -0500

#3420 — who can eplain deeply the ruby's methods?

Fromsavin max <mafei.198@gmail.com>
Date2011-04-24 00:57 -0500
Subjectwho can eplain deeply the ruby's methods?
Message-ID<999670eb7a464b9d106f84fb7385d075@ruby-forum.com>
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/.

[toc] | [next] | [standalone]


#3426

FromStu <stu@rubyprogrammer.net>
Date2011-04-24 03:46 -0500
Message-ID<BANLkTik5+tZF74DiU1mhd5ikkCANgUh__g@mail.gmail.com>
In reply to#3420
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 <mafei.198@gmail.com> 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/.
>

[toc] | [prev] | [next] | [standalone]


#3427

FromStu <stu@rubyprogrammer.net>
Date2011-04-24 03:58 -0500
Message-ID<BANLkTimLnHswJDp1LdMmHoAO+zaMF361-g@mail.gmail.com>
In reply to#3426
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
 => #<MyClass:0x00000002d55618>
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 <stu@rubyprogrammer.net> 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 <mafei.198@gmail.com> 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/.
>>
>

[toc] | [prev] | [next] | [standalone]


#3431

Fromsavin max <mafei.198@gmail.com>
Date2011-04-24 08:18 -0500
Message-ID<e66077c212ee61d83ca98cea7e0fc6a6@ruby-forum.com>
In reply to#3426
thank you very much.
your answer is good for me.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3446

FromStu <stu@rubyprogrammer.net>
Date2011-04-24 15:53 -0500
Message-ID<BANLkTikXkRcQ30Cm2uaaLwZQDMtv=sAPVA@mail.gmail.com>
In reply to#3431
glad to help. I wrote that right before I went to sleep last night. If
I could edit the post (which I can't) I'd remove my lazy run on
sentences. I'd also rewrite the array/hash explanation to say this:

Another great example would be Array and Hash. Both data types share
Enumerable and I'm sure both override the .each method to deal with
the polymorphism of either being an indexed array or an associative
array. In the end they are just containers. Container data structures
that are purely abstracted and feature endowed linked lists. This
paradigm shift blurs the distinction of Array and Hash through a
clever abstraction which proves Ruby to be absolutely "principle of
least surprise"

*.each example with a simple hash and multidimensional array :

>> {one: 1, two:2}.each {|x,y| p x; p y}
:one
1
:two
2
 => {:one=>1, :two=>2}

>> [[:one,1],[:two,2]].each{|x,y| p x; p y}
:one
1
:two
2
 => [[:one, 1], [:two, 2]]

A better example would be to put both the array and hash into a
variable where one wouldn't even have to know what the variable data
type was  but knew they could parse it without worry of implementation
details or test with a million + one conditionals to figure out how to
deal with the data.

Yukihiro Matsumoto truly has engineered an elaborate and eloquent
language which elucidates the intricate nature of object oriented
programming. I can't begin to explain how much respect I have for this
man.

On Sun, Apr 24, 2011 at 8:18 AM, savin max <mafei.198@gmail.com> wrote:
> thank you very much.
> your answer is good for me.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

[toc] | [prev] | [next] | [standalone]


#3449

Fromsavin max <mafei.198@gmail.com>
Date2011-04-24 22:10 -0500
Message-ID<41787478075d1b2ca71a41f057bbeba3@ruby-forum.com>
In reply to#3446
Stu wrote in post #994785:
>
> *.each example with a simple hash and multidimensional array :
>
>>> {one: 1, two:2}.each {|x,y| p x; p y}
> :one
> 1
> :two
> 2
>  => {:one=>1, :two=>2}
>
>>> [[:one,1],[:two,2]].each{|x,y| p x; p y}
> :one
> 1
> :two
> 2
>  => [[:one, 1], [:two, 2]]
>
> A better example would be to put both the array and hash into a
> variable where one wouldn't even have to know what the variable data
> type was  but knew they could parse it without worry of implementation
> details or test with a million + one conditionals to figure out how to
> deal with the data.

I have just read your questions at this place, i think you are a deep 
thinking person.

thank you!~

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3484

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-25 19:35 -0500
Message-ID<1c8e2ee34b928b86ec8411f584039fd4@ruby-forum.com>
In reply to#3420
savin max wrote in post #994702:
>
> what's the relationship between these methods?
> #e.g.
>     String.instance_methods == "str".methods #true
>

That isn't a necessary relationship--it just happens to be true in that 
case.

1) methods() returns all the methods that an object will respond to.

2) instance_methods() returns all the instance methods defined in a 
class, plus all methods that are included in a class, plus all the 
instance methods that are inherited from superclasses(including methods 
that are included by superclasses).

What's the difference between 1) and 2)?

str = 'abc'

def str.hello
  puts 'hi'
end

The object str will respond to the :hello message (i.e. you can call 
hello() on str).  Therefore, hello() will be listed in the array 
returned by methods().  However, 'hello' is not an instance method 
because it was not defined in a regular class or a module.  Therefore, 
hello() will not be returned by String.instance_methods().

p String.instance_methods.grep(/^h/)

--output:--
[:hash, :hex]


>     String.methods(false) == String.singleton_methods(false)  #true
>     class method is the singleton method?
> .....

Yes 'class methods' are just singleton methods of an object, when the 
object is a class.  (Can you really call methods() with a false 
argument?  Of course, I can't find any ruby documentation on the 
methods() method, so who knows?)



class Dog

  def initialize(name)
    @name = name
  end

  def do_stuff(other)
    puts 'do_stuff'

    my_private_meth()

    other.my_protected_meth()
  end

  protected
  def my_protected_meth
    puts @name
  end

  private
  def my_private_meth
    puts 'private meth'
  end

end


dogA = Dog.new("7stud")

puts "methods():"
p dogA.methods

puts "Dog.instance_methods():"
not_inherited = false
p Dog.instance_methods(not_inherited)

puts "Dog.public_instance_methods():"
p Dog.public_instance_methods(not_inherited)

puts "Dog.protected_instance_methods():"
p Dog.protected_instance_methods(not_inherited)

puts "Dog.private_instance_methods():"
p Dog.private_instance_methods(not_inherited)


--output:--
methods():
[:do_stuff, :my_protected_meth, :nil?, :===, :=~, :!~, :eql?, :hash, 
:<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, 
:initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, 
:trust, :freeze, :frozen?, :to_s, :inspect, :methods, 
:singleton_methods, :protected_methods, :private_methods, 
:public_methods, :instance_variables, :instance_variable_get, 
:instance_variable_set, :instance_variable_defined?, :instance_of?, 
:kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, 
:respond_to_missing?, :extend, :display, :method, :public_method, 
:define_singleton_method, :__id__, :object_id, :to_enum, :enum_for, :==, 
:equal?, :!, :!=, :instance_eval, :instance_exec, :__send__]
Dog.instance_methods():
[:do_stuff, :my_protected_meth]
Dog.public_instance_methods():
[:do_stuff]
Dog.protected_instance_methods():
[:my_protected_meth]
Dog.private_instance_methods():
[:initialize, :my_private_meth]

=======


dogA = Dog.new("7stud")
dogA.my_private_meth

--output:--
prog.rb:49:in `<main>': private method `my_private_meth' called for 
#<Dog:0x9cf1684 @name="7stud"> (NoMethodError)


dogA.my_protected_meth

--output:--
prog.rb:49:in `<main>': protected method `my_protected_meth' called for 
#<Dog:0x96546a0 @name="7stud"> (NoMethodError)



dogB = Dog.new("savin")
dogA.do_stuff(dogB)

--output:--
do_stuff
private meth
savin

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3486

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-25 19:57 -0500
Message-ID<7b31d92cd9f02bc49d4ea27e46c14de7@ruby-forum.com>
In reply to#3420
a) Singleton methods are inserted into an anonymous class immediately 
above the object in the method lookup chain.

b) The methods in an included module are also inserted in an anonymous 
class immediately above the class/module that does the include()'ing.

One tricky part of the method lookup: class methods are inherited by 
subclasses:

class Parent
  def self.greet
    puts 'Hi!'
  end
end

class Child < Parent
  def self.cry
    puts 'Weeep, weep!'
  end
end

Child.cry
Child.greet

--output:--
Weeep, weep!
Hi!

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3507

Fromsavin max <mafei.198@gmail.com>
Date2011-04-26 05:06 -0500
Message-ID<2497ecda6bc30826169863672f7dccfc@ruby-forum.com>
In reply to#3486
7stud -- wrote in post #994995:



Thank you 7stud~

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web