find all member in a class

P

Pokkai Dokkai

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

Robert Klemme

2007/10/26 said:
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
 
P

Paul

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

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
 
R

Rick DeNatale

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

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.
 
T

Thufir

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
 
T

trebor777

THUFIR said:
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



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
 
R

Rick DeNatale

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,338
Members
48,028
Latest member
chasetony

Latest Threads

Top