# Find all member in a class

**URL:** <https://www.ruby-forum.com/t/find-all-member-in-a-class/118375>\
**Category:** Ruby\
**Created:** [October 26, 2007, 9:16am UTC](https://www.ruby-forum.com/t/find-all-member-in-a-class/118375 "2007-10-26T09:16:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 26, 2007, 9:16am UTC](https://www.ruby-forum.com/t/find-all-member-in-a-class/118375/1 "2007-10-26T09:16:01Z")

</div>

how to find all members list(member functions & member variables for all  
access specifiers like public, private and protected) ?

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 26, 2007, 11:08am UTC](https://www.ruby-forum.com/t/find-all-member-in-a-class/118375/2 "2007-10-26T11:08:50Z")

</div>

2007/10/26, Pokkai D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> how to find all members list(member functions & member variables for all  
> access specifiers like public, private and protected) ?

Do

ruby -e ‘puts String.methods.grep(/meth/)’

and go from there.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 26, 2007, 11:35am UTC](https://www.ruby-forum.com/t/find-all-member-in-a-class/118375/3 "2007-10-26T11:35:45Z")

</div>

On Oct 26, 5:16 pm, Pokkai D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## how to find all members list(member functions & member variables for all access specifiers like public, private and protected) ?
> 
> Posted viahttp://www.ruby-forum.com/.

# irb(main):002:0\> puts String.methods.sort \< \<= \<=\>

===  
=~

> =  
> **id**  
> **send**  
> allocate  
> ancestors  
> autoload  
> autoload?  
> class  
> class\_eval  
> class\_variable\_defined?  
> class\_variables  
> clone  
> const\_defined?  
> const\_get  
> const\_missing  
> const\_set  
> constants  
> display  
> dup  
> eql?  
> equal?  
> extend  
> freeze  
> frozen?  
> gem  
> hash  
> id  
> include?  
> included\_modules  
> inspect  
> instance\_eval  
> instance\_method  
> instance\_methods  
> instance\_of?  
> instance\_variable\_defined?  
> instance\_variable\_get  
> instance\_variable\_set  
> instance\_variables  
> is\_a?  
> kind\_of?  
> method  
> method\_defined?  
> methods  
> module\_eval  
> name  
> new  
> nil?  
> object\_id  
> private\_class\_method  
> private\_instance\_methods  
> private\_method\_defined?  
> private\_methods  
> protected\_instance\_methods  
> protected\_method\_defined?  
> protected\_methods  
> public\_class\_method  
> public\_instance\_methods  
> public\_method\_defined?  
> public\_methods  
> require  
> require\_gem  
> respond\_to?  
> send  
> singleton\_methods  
> superclass  
> taguri  
> taguri=  
> taint  
> tainted?  
> to\_a  
> to\_s  
> to\_yaml  
> to\_yaml\_properties  
> to\_yaml\_style  
> type  
> untaint  
> yaml\_as  
> yaml\_new  
> yaml\_tag\_class\_name  
> yaml\_tag\_read\_class  
> yaml\_tag\_subclasses?  
> =\> nil

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 26, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/find-all-member-in-a-class/118375/4 "2007-10-26T13:58:42Z")

</div>

On 10/26/07, Pokkai D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## how to find all members list(member functions & member variables for all access specifiers like public, private and protected) ?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Note that unlike some languages with which you might be familiar,  
“member” variables aren’t declared and only come into being when  
defined during code execution. In fact there’s a real difference  
between that subtle vocabulary shift between “member” and “instance”.  
Different instances of the same class might have different sets of  
instance variables. In fact the same instance might acquire instance  
variables over time:

class A  
def i  
@i ||= 42  
end

def j  
@j ||= “hello”  
end  
end

a1 = A.new

a1.instance\_variables # =\> []

a1.i

a1.instance\_variables # =\> [“@i”]

a1.j

a1.instance\_variables # =\> [“@j”, “@i”]

AND, since Ruby allows instances to have singleton methods, it’s even  
possible for two instances of the same class to have different sets of  
methods.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 27, 2007, 1:08pm UTC](https://www.ruby-forum.com/t/find-all-member-in-a-class/118375/5 "2007-10-27T13:08:01Z")

</div>

On Fri, 26 Oct 2007 20:58:01 +0900, Rick DeNatale wrote:

> class A  
> def i  
> @i ||= 42  
> end

Some rudimentary explanation please:

the def is to define a method?  
it’s a coincidence that the method name is “i” and the instance variable  
has the same name, “I”?

the third line declares an instance variable, “i”, what do the “||” do  
in  
that line, please?

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 27, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/find-all-member-in-a-class/118375/6 "2007-10-27T13:23:14Z")

</div>

THUFIR HAWAT wrote:

> Some rudimentary explanation please:
> 
> thanks,
> 
> Thufir

Yeap “def” is to define a method  
It could be a coincidence… or not, depends on what you want to do with  
this method… hum Here it’s not a coincidence… this method return the  
value of the instance variable i (it’s a reader accessor) or if it’s  
nil,  
set it to 42 and return it.

basically || means "if it’s nil do that ", then = assigns the value 42  
to @i

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [October 27, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/find-all-member-in-a-class/118375/7 "2007-10-27T16:00:00Z")

</div>

On 10/27/07, trebor777 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > @i ||= 42
> > > 
> > > ```
> > 
> > the third line declares an instance variable, “i”, what do the “||” do in
> 
> Yeap “def” is to define a method  
> It could be a coincidence… or not, depends on what you want to do with  
> this method… hum Here it’s not a coincidence… this method return the  
> value of the instance variable i (it’s a reader accessor) or if it’s nil,  
> set it to 42 and return it.
> 
> basically || means "if it’s nil do that ", then = assigns the value 42 to @i

Correct, it’s an attribute reader with lazy initialization of the  
instance variable.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
