# Returning the names of each defined method

**URL:** <https://www.ruby-forum.com/t/returning-the-names-of-each-defined-method/149288>\
**Category:** Ruby\
**Created:** [September 23, 2008, 9:59am UTC](https://www.ruby-forum.com/t/returning-the-names-of-each-defined-method/149288 "2008-09-23T09:59:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kain\_nobel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dec6dc/32.png) [@kain\_nobel](https://www.ruby-forum.com/u/kain_nobel)\
**Post date:** [September 23, 2008, 9:59am UTC](https://www.ruby-forum.com/t/returning-the-names-of-each-defined-method/149288/1 "2008-09-23T09:59:34Z")

</div>

Good day everybody!

I was curious, for code that isn’t veiwable, how would I print each  
defined method in a class/module? For instance… lets say that File  
happened to be a hidden class, how could I make the interpreter p or  
print each method that is defined in class File?

---

<div class="post-metadata">

**Author:** ![kain\_nobel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dec6dc/32.png) [@kain\_nobel](https://www.ruby-forum.com/u/kain_nobel)\
**Post date:** [September 23, 2008, 11:18am UTC](https://www.ruby-forum.com/t/returning-the-names-of-each-defined-method/149288/2 "2008-09-23T11:18:55Z")

</div>

Kain Nobel wrote:

> I was curious, for code that isn’t veiwable, how would I print each  
> defined method in a class/module? For instance… lets say that File  
> happened to be a hidden class, how could I make the interpreter p or  
> print each method that is defined in class File?

What do you mean by a “hidden class”, or “code that isn’t viewable”? If  
the Ruby interpreter is able to read a .rb file, then so are you.

But in any case there are a number of reflection methods, for example:

irb(main):001:0\> File.instance\_methods - Object.instance\_methods  
=\> [“select”, “read”, “\<\<”, “lineno=”, “tell”, “mtime”, “fileno”,  
“partition”, “print”, “rewind”, “pid”, “readpartial”, “grep”,  
“readlines”, “stat”, “truncate”, “tty?”, “seek”, “reject”, “getc”,  
“binmode”, “to\_io”, “ctime”, “pos”, “putc”, “closed?”, “member?”,  
“readchar”, “find”, “pos=”, “close\_read”, “each\_with\_index”,  
“each\_line”, “lstat”, “reopen”, “to\_i”, “collect”, “sysseek”, “all?”,  
“entries”, “fsync”, “puts”, “eof”, “detect”, “ungetc”, “zip”,  
“each\_byte”, “flock”, “close\_write”, “syswrite”, “map”, “any?”, “ioctl”,  
“chmod”, “sync”, “sort”, “include?”, “min”, “gets”, “sync=”, “flush”,  
“find\_all”, “write”, “isatty”, “each”, “atime”, “sysread”, “inject”,  
“printf”, “eof?”, “fcntl”, “path”, “sort\_by”, “chown”, “lineno”, “max”,  
“readline”, “close”]

For more info see  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/ospace.html](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ospace.html)

---

<div class="post-metadata">

**Author:** ![kain\_nobel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dec6dc/32.png) [@kain\_nobel](https://www.ruby-forum.com/u/kain_nobel)\
**Post date:** [September 23, 2008, 11:23am UTC](https://www.ruby-forum.com/t/returning-the-names-of-each-defined-method/149288/3 "2008-09-23T11:23:49Z")

</div>

Kain Nobel wrote:

> Good day everybody!
> 
> I was curious, for code that isn’t veiwable, how would I print each  
> defined method in a class/module? For instance… lets say that File  
> happened to be a hidden class, how could I make the interpreter p or  
> print each method that is defined in class File?

p File.methods  
#or  
puts File.methods.sort

# you can’t use all these methods, some of them are private. To exclude

these:

puts File.public\_methods.sort

# Works on instances, too.

puts “somestring”.public\_methods.sort

hth,

Siep
