[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

find all member in a class

Pokkai Dokkai

10/26/2007 7:16:00 AM

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

6 Answers

Robert Klemme

10/26/2007 9:08:00 AM

0

2007/10/26, Pokkai Dokkai <bad_good_lion@yahoo.com>:
> 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

Paul

10/26/2007 9:32:00 AM

0

On Oct 26, 5:16 pm, Pokkai Dokkai <bad_good_l...@yahoo.com> wrote:
> how to find all members list(member functions & member variables for all
> access specifiers like public, private and protected) ?
> --
> Posted viahttp://www.ruby-....

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

Rick DeNatale

10/26/2007 11:58:00 AM

0

On 10/26/07, Pokkai Dokkai <bad_good_lion@yahoo.com> 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-....
>
>

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

Thufir Hawat

10/27/2007 11:07:00 AM

0

trebor777

10/27/2007 11:23:00 AM

0




THUFIR HAWAT wrote:
>
> 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
>
>
>
>



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


--
View this message in context: http://www.nabble.com/find-all-member-in-a-class-tf4695579.html...
Sent from the ruby-talk mailing list archive at Nabble.com.


Rick DeNatale

10/27/2007 1:59:00 PM

0

On 10/27/07, trebor777 <mrobert@trebor777.net> wrote:
>
>
>
> THUFIR HAWAT wrote:
> >
> > 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
> >
> >
> >
> >
>
>
>
> 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.denh...