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