Find all member in a class

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

2007/10/26, Pokkai D. [email protected]:

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

On Oct 26, 5:16 pm, Pokkai D. [email protected] 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

On 10/26/07, Pokkai D. [email protected] 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/.

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/

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

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

On 10/27/07, trebor777 [email protected] 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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs