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


Groups > comp.lang.ruby > #3484

Re: who can eplain deeply the ruby's methods?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail
From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: who can eplain deeply the ruby's methods?
Date Mon, 25 Apr 2011 19:35:59 -0500
Organization Service de news de lacave.net
Lines 146
Message-ID <1c8e2ee34b928b86ec8411f584039fd4@ruby-forum.com> (permalink)
References <999670eb7a464b9d106f84fb7385d075@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1303778183 27395 65.111.164.187 (26 Apr 2011 00:36:23 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Tue, 26 Apr 2011 00:36:23 +0000 (UTC)
In-Reply-To <999670eb7a464b9d106f84fb7385d075@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 382172
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <1c8e2ee34b928b86ec8411f584039fd4@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:3484

Show key headers only | View raw


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/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web