Quarks


#1

Why?

class X; end

X.methods(false)
=> []

But…

X.public_methods(false)
=> [“allocate”, “superclass”, “new”]

X.private_methods(false)
=> [“initialize”, “initialize_copy”, “inherited”]

T.


#2

On Jul 28, 2007, at 11:21 AM, Trans wrote:

=> [“allocate”, “superclass”, “new”]

X.private_methods(false)
=> [“initialize”, “initialize_copy”, “inherited”]

T.

Inheritance.
Inherits from Object


#3

On Jul 28, 9:41 am, John J. removed_email_address@domain.invalid
wrote:

Inheritance.
Inherits from Object

But the (false) parameter is supposed to limit it to just the class,
no ancestors. And it does mostly. But “mostly” is what doesn’t make
sense. If you use (true) instead you get:

X.public_methods(true)
=> [“inspect”, “private_class_method”, “const_missing”, “clone”,
“method”, “public_methods”, “public_instance_methods”,
“instance_variable_defined?”, “method_defined?”, “superclass”,
“equal?”, “freeze”, “included_modules”, “const_get”, “methods”,
“respond_to?”, “module_eval”, “class_variables”, “dup”,
“protected_instance_methods”, “instance_variables”,
“public_method_defined?”, “id”, “eql?”, “object_id”, “const_set”,
“id”, “singleton_methods”, “send”, “class_eval”, “taint”, “frozen?”,
“instance_variable_get”, “include?”, “private_instance_methods”,
send”, “instance_of?”, “private_method_defined?”, “to_a”, “name”,
“autoload”, “type”, “new”, “<”, “protected_methods”, “instance_eval”,
“<=>”, “display”, “==”, “>”, “===”, “instance_method”,
“instance_variable_set”, “kind_of?”, “extend”,
“protected_method_defined?”, “const_defined?”, “>=”, “ancestors”,
“to_s”, “<=”, “public_class_method”, “allocate”, “hash”, “class”,
“instance_methods”, “tainted?”, “=~”, “private_methods”,
“class_variable_defined?”, “nil?”, “untaint”, “constants”, “is_a?”,
“autoload?”]

X.private_methods(true)
=> [“select”, “global_variables”, “attr”, “warn”, “initialize”,
“readline”, “singleton_method_added”, “gsub”, “exit!”, “public”,
“method_missing”, “method_added”, “alias_method”, “abort”, “exec”,
“print”, “remove_instance_variable”, “chomp!”, “load”, “eval”,
“srand”, “proc”, “untrace_var”, “Integer”, “local_variables”,
“attr_reader”, “readlines”, “singleton_method_removed”, “inherited”,
“raise”, “chop”, “protected”, “getc”, “define_method”,
“method_removed”, “system”, “at_exit”, “require”, “putc”, “test”,
“set_trace_func”, “lambda”, “rand”, “attr_writer”, “Float”,
“remove_class_variable”, “initialize_copy”, “p”,
“singleton_method_undefined”, “chomp”, “fail”, “syscall”, “callcc”,
“sub!”, “private”, “method_undefined”, “iterator?”, “catch”, “sleep”,
“puts”, “`”, “String”, “attr_accessor”, “sprintf”, “remove_method”,
“class_variable_get”, “included”, “split”, “caller”, “irb_binding”,
“gsub!”, “open”, “block_given?”, “throw”, “remove_const”, “gets”,
“trap”, “sub”, “loop”, “include”, “Array”, “fork”, “undef_method”,
“class_variable_set”, “extended”, “format”, “exit”, “printf”, “chop!”,
“trace_var”, “binding”, “scan”]

T.


#4

On Jul 28, 2007, at 12:28 PM, Trans wrote:

sense. If you use (true) instead you get:

Check the docs again
ri methods

--------------------------------------------------------- Object#methods
obj.methods => array

  Returns a list of the names of methods publicly accessible in
  _obj_. This will include all the methods accessible in _obj_'s
  ancestors.

ri public_methods

-------------------------------------------------- Object#public_methods
obj.public_methods(all=true) => array

  Returns the list of public methods accessible to _obj_. If the
  _all_ parameter is set to +false+, only those methods in the
  receiver will be listed.

So it matters what it means that “methods in the receiver” will be
listed.
some methods are built in to every object.
You can make them private if you need to but then what good would
they be?
Sending .methods(false) will just give you an empty array. Not useful.

Somethings to read about in ri :
Object
Kernel
Singleton

These classes are pretty important to understanding some of these
aspects of the Ruby object model and inheritance.
Read those carefully and it will be a little more clear.


#5

On 7/28/07, Trans removed_email_address@domain.invalid wrote:

=> [“allocate”, “superclass”, “new”]

X.private_methods(false)
=> [“initialize”, “initialize_copy”, “inherited”]

T.

irb(main):001:0> class X;end
=> nil
irb(main):002:0> x=X.new
=> #<X:0xb7d7c21c>
irb(main):003:0> x.methods false
=> []
irb(main):004:0> x.public_methods false
=> []
irb(main):005:0> x.private_methods false
=> []

seems quite ok.
The result of
X.public_methods false and
X.private_methods false
seems correct
but why the hack :wink: is
X.methods false
empty???

Robert


#6

John J. wrote:

ri methods

--------------------------------------------------------- Object#methods
obj.methods => array

 Returns a list of the names of methods publicly accessible in
 _obj_. This will include all the methods accessible in _obj_'s
 ancestors.

Unfortunately that doesn’t tell us that it takes an optional argument,
and that if the argument is false, the method returns the result of
obj.singleton_methods(false), which is just the singleton methods
defined on the object.

Looks like a doc bug…


#7

On 7/28/07, Robert D. removed_email_address@domain.invalid wrote:

X.public_methods(false)
irb(main):001:0> class X;end
seems quite ok.
The following is the uppercase X, sorry very bad naming of my part :(.


#8

Hi,

At Sun, 29 Jul 2007 01:21:46 +0900,
Trans wrote in [ruby-talk:262287]:

Why?

Maybe a bug.

Index: class.c

— class.c (revision 12852)
+++ class.c (working copy)
@@ -600,9 +600,18 @@ class_instance_method_list(int argc, VAL

 list = st_init_numtable();
  • for (; mod; mod = RCLASS(mod)->super) {
  • if (mod) {
    st_foreach(RCLASS(mod)->m_tbl, method_entry, (st_data_t)list);
  • if (BUILTIN_TYPE(mod) == T_ICLASS) continue;
  • if (FL_TEST(mod, FL_SINGLETON)) continue;
  • if (!recur) break;
  • if (recur) {
  •  while ((mod = RCLASS(mod)->super) != 0) {
    
  • st_foreach(RCLASS(mod)->m_tbl, method_entry, (st_data_t)list);
  •  }
    
  • }
    +#define HIDDEN_CLASS_P(mod) ((BUILTIN_TYPE(mod) == T_ICLASS) || \
  •       (FL_TEST(mod, FL_SINGLETON)))
    
  • else if (HIDDEN_CLASS_P(mod)) {
  •  while ((mod = RCLASS(mod)->super) != 0 && HIDDEN_CLASS_P(mod)) {
    
  • st_foreach(RCLASS(mod)->m_tbl, method_entry, (st_data_t)list);
  •  }
    
  • }
    }
    ary = rb_ary_new();

#9

On Jul 28, 2:17 pm, Nobuyoshi N. removed_email_address@domain.invalid wrote:

===================================================================

  •   if (!recur) break;
    
  •       }
    
  •   }
    
    }
    ary = rb_ary_new();

Thanks Nobu. Though that code seems kind of odd too. Does ‘recur’
refer to the ancestors flag? If so, how does the HIDDEN_CLASS_P (he
he, another name for singleton) exclude the ancestors?

T.


#10

Hi,

At Sun, 29 Jul 2007 19:54:58 +0900,
Trans wrote in [ruby-talk:262375]:

Thanks Nobu. Though that code seems kind of odd too. Does ‘recur’
refer to the ancestors flag? If so, how does the HIDDEN_CLASS_P (he
he, another name for singleton) exclude the ancestors?

If recur is true, all ancestors are included. If false, search
before an ordinary class. Still I’m not sure about this
behavior, singleton methods include methods from modules,
though.