# Quarks

**URL:** <https://rubytalk.org/t/quarks/39483>\
**Category:** ruby-talk\
**Created:** [28 July 2007 16:21 UTC](https://rubytalk.org/t/quarks/39483 "2007-07-28T16:21:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [28 July 2007 16:21 UTC](https://rubytalk.org/t/quarks/39483/1 "2007-07-28T16:21:46Z")

</div>

Why?

&nbsp;&nbsp;class X; end

&nbsp;&nbsp;X.methods(false)  
&nbsp;&nbsp;=\> []

But...

&nbsp;&nbsp;X.public\_methods(false)  
&nbsp;&nbsp;=\> ["allocate", "superclass", "new"]

&nbsp;&nbsp;X.private\_methods(false)  
&nbsp;&nbsp;=\> ["initialize", "initialize\_copy", "inherited"]

T.

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [28 July 2007 16:41 UTC](https://rubytalk.org/t/quarks/39483/2 "2007-07-28T16:41:34Z")

</div>

Inheritance.  
Inherits from Object

> **···**
>
> On Jul 28, 2007, at 11:21 AM, Trans wrote:
> 
> > Why?
> > 
> > &nbsp;&nbsp;class X; end
> > 
> > &nbsp;&nbsp;X.methods(false)  
> > &nbsp;&nbsp;=\>
> > 
> > But...
> > 
> > &nbsp;&nbsp;X.public\_methods(false)  
> > &nbsp;&nbsp;=\> ["allocate", "superclass", "new"]
> > 
> > &nbsp;&nbsp;X.private\_methods(false)  
> > &nbsp;&nbsp;=\> ["initialize", "initialize\_copy", "inherited"]
> > 
> > T.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [28 July 2007 20:32 UTC](https://rubytalk.org/t/quarks/39483/3 "2007-07-28T20:32:26Z")

</div>

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 😉 is  
X.methods false  
empty???

Robert

> **···**
>
> On 7/28/07, Trans \<transfire@gmail.com\> wrote:
> 
> > Why?
> > 
> > &nbsp;&nbsp;class X; end
> > 
> > &nbsp;&nbsp;X.methods(false)  
> > &nbsp;&nbsp;=\>
> > 
> > But...
> > 
> > &nbsp;&nbsp;X.public\_methods(false)  
> > &nbsp;&nbsp;=\> ["allocate", "superclass", "new"]
> > 
> > &nbsp;&nbsp;X.private\_methods(false)  
> > &nbsp;&nbsp;=\> ["initialize", "initialize\_copy", "inherited"]
> > 
> > T.
> 
> --  
> [...] as simple as possible, but no simpler.  
> -- Attributed to Albert Einstein

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [28 July 2007 21:17 UTC](https://rubytalk.org/t/quarks/39483/4 "2007-07-28T21:17:36Z")

</div>

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
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list = st\_init\_numtable();  
> - for (; mod; mod = RCLASS(mod)-\>super) {  
> + if (mod) {  
> &nbsp;&nbsp;&nbsp;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);  
> + }  
> + }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ary = rb\_ary\_new();  
>   
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [28 July 2007 17:28 UTC](https://rubytalk.org/t/quarks/39483/5 "2007-07-28T17:28:51Z")

</div>

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.

> **···**
>
> On Jul 28, 9:41 am, John Joyce \<dangerwillrobinsondan...@gmail.com\> wrote:
> 
> > Inheritance.  
> > Inherits from Object

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [28 July 2007 20:33 UTC](https://rubytalk.org/t/quarks/39483/6 "2007-07-28T20:33:33Z")

</div>

> \> Why?  
> \>  
> \> class X; end  
> \>  
> \> X.methods(false)  
> \> =\>   
> \>  
> \> But...  
> \>  
> \> X.public\_methods(false)  
> \> =\> ["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 following is the \*uppercase\* X, sorry very bad naming of my part :(.

> **···**
>
> On 7/28/07, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > On 7/28/07, Trans \<transfire@gmail.com\> wrote:  
> > The result of  
> > X.public\_methods false and  
> > X.private\_methods false  
> > seems correct  
> > but why the hack 😉 is  
> > X.methods false  
> > empty???
> > 
> > Robert  
> > --  
> > [...] as simple as possible, but no simpler.  
> > -- Attributed to Albert Einstein
> 
> --  
> [...] as simple as possible, but no simpler.  
> -- Attributed to Albert Einstein

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [29 July 2007 10:54 UTC](https://rubytalk.org/t/quarks/39483/7 "2007-07-29T10:54:58Z")

</div>

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.

> **···**
>
> On Jul 28, 2:17 pm, Nobuyoshi Nakada \<n...@ruby-lang.org\> wrote:
> 
> > 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
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list = st\_init\_numtable();  
> > - for (; mod; mod = RCLASS(mod)-\>super) {  
> > + if (mod) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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);  
> > + }  
> > + }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ary = rb\_ary\_new();

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [28 July 2007 19:27 UTC](https://rubytalk.org/t/quarks/39483/8 "2007-07-28T19:27:16Z")

</div>

Check the docs again  
ri methods

--------------------------------------------------------- Object#methods  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj.methods =\> array

> **···**
>
> On Jul 28, 2007, at 12:28 PM, Trans wrote:
> 
> > On Jul 28, 9:41 am, John Joyce \<dangerwillrobinsondan...@gmail.com\> \> 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:
> 
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns a list of the names of methods publicly accessible in  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_obj\_. This will include all the methods accessible in \_obj\_'s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ancestors.
> 
> ri public\_methods
> 
> -------------------------------------------------- Object#public\_methods  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj.public\_methods(all=true) =\> array  
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the list of public methods accessible to \_obj\_. If the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_all\_ parameter is set to +false+, only those methods in the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [29 July 2007 12:50 UTC](https://rubytalk.org/t/quarks/39483/9 "2007-07-29T12:50:46Z")

</div>

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.

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [28 July 2007 20:20 UTC](https://rubytalk.org/t/quarks/39483/10 "2007-07-28T20:20:59Z")

</div>

John Joyce wrote:

> ri methods
> 
> --------------------------------------------------------- Object#methods  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj.methods =\> array  
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns a list of the names of methods publicly accessible in  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_obj\_. This will include all the methods accessible in \_obj\_'s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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....

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
