[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Naming ruby class attribute and methods

Demonic Software

3/14/2008 3:20:00 AM

[Note: parts of this message were removed to make it a legal post.]

Hello,

Is there a way to name all the methods and attributes/ class instantiated
variables that have been created? For example, from the class below, I
would like to get all the variables instantiated from the initialize and
example methods, and then I would also like to be able to get the method
names.


#pseudo code, not sure if it really runs
class Foo
def initialize()
@thestring = ''
@haha = "HA HA HAA!"
@var = "I am a variable"
end
def example(arg)
@arg = arg
@thestring = "#{@haha} #{@var}: #{arg}"
end
def thestring
@thestring
end
end


So is there a way to extract ["thestring", "var", "haha", "arg"] for the
variable names and then ["initialize", "example", "thestring"] for the
method names. I can see how I would do it with regular expressions and
ruby2ruby, but I was wondering if there was another way of extracting the
information. Thanks in advance.

5 Answers

David A. Black

3/14/2008 3:27:00 AM

0

Hi --

On Fri, 14 Mar 2008, Demonic Software wrote:

> Hello,
>
> Is there a way to name all the methods and attributes/ class instantiated
> variables that have been created? For example, from the class below, I
> would like to get all the variables instantiated from the initialize and
> example methods, and then I would also like to be able to get the method
> names.
>
>
> #pseudo code, not sure if it really runs
> class Foo
> def initialize()
> @thestring = ''
> @haha = "HA HA HAA!"
> @var = "I am a variable"
> end
> def example(arg)
> @arg = arg
> @thestring = "#{@haha} #{@var}: #{arg}"
> end
> def thestring
> @thestring
> end
> end
>
>
> So is there a way to extract ["thestring", "var", "haha", "arg"] for the
> variable names and then ["initialize", "example", "thestring"] for the
> method names. I can see how I would do it with regular expressions and
> ruby2ruby, but I was wondering if there was another way of extracting the
> information. Thanks in advance.

You can get method and instance variable names. An "attribute" is
really a kind of virtual construct, composed of methods and (usually)
instance variables, so it doesn't appear as a separate thing when you
do introspective stuff on the object. The main time "attribute" exists
is when you create them; after that, they're just methods (though one
is of course free to continue to call them attributes).

Foo.instance_methods(false) will give you all the instance methods
defined in Foo. (The rather cryptic "false" means: don't include
methods created higher in the ancestry.) To get the instance variables
you have to instantiate the class, run the methods that create them,
and then call the #instance_variables method. (Just defining methods
with i. vars inside them doesn't create the i. vars.)


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!

Demonic Software

3/14/2008 3:40:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Thanks David.
Is there any way to get the methods on an object that is already
instantiated?

Thanks again

On Thu, Mar 13, 2008 at 10:27 PM, David A. Black <dblack@rubypal.com> wrote:

> Hi --
>
> On Fri, 14 Mar 2008, Demonic Software wrote:
>
> > Hello,
> >
> > Is there a way to name all the methods and attributes/ class
> instantiated
> > variables that have been created? For example, from the class below, I
> > would like to get all the variables instantiated from the initialize and
> > example methods, and then I would also like to be able to get the method
> > names.
> >
> >
> > #pseudo code, not sure if it really runs
> > class Foo
> > def initialize()
> > @thestring = ''
> > @haha = "HA HA HAA!"
> > @var = "I am a variable"
> > end
> > def example(arg)
> > @arg = arg
> > @thestring = "#{@haha} #{@var}: #{arg}"
> > end
> > def thestring
> > @thestring
> > end
> > end
> >
> >
> > So is there a way to extract ["thestring", "var", "haha", "arg"] for the
> > variable names and then ["initialize", "example", "thestring"] for the
> > method names. I can see how I would do it with regular expressions and
> > ruby2ruby, but I was wondering if there was another way of extracting
> the
> > information. Thanks in advance.
>
> You can get method and instance variable names. An "attribute" is
> really a kind of virtual construct, composed of methods and (usually)
> instance variables, so it doesn't appear as a separate thing when you
> do introspective stuff on the object. The main time "attribute" exists
> is when you create them; after that, they're just methods (though one
> is of course free to continue to call them attributes).
>
> Foo.instance_methods(false) will give you all the instance methods
> defined in Foo. (The rather cryptic "false" means: don't include
> methods created higher in the ancestry.) To get the instance variables
> you have to instantiate the class, run the methods that create them,
> and then call the #instance_variables method. (Just defining methods
> with i. vars inside them doesn't create the i. vars.)
>
>
> David
>
> --
> Upcoming Rails training from David A. Black and Ruby Power and Light:
> ADVANCING WITH RAILS, April 14-17 2008, New York City
> CORE RAILS, June 24-27 2008, London (Skills Matter)
> See http://www.r... for details. Berlin dates coming soon!
>
>

David A. Black

3/14/2008 3:44:00 AM

0

Hi --

On Fri, 14 Mar 2008, Demonic Software wrote:

> Thanks David.
> Is there any way to get the methods on an object that is already
> instantiated?

Yes: object.methods


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!

Todd Benson

3/14/2008 4:24:00 AM

0

On Thu, Mar 13, 2008 at 10:39 PM, Demonic Software
<demonic.software@gmail.com> wrote:
> Thanks David.
> Is there any way to get the methods on an object that is already
> instantiated?
>
> Thanks again
>
>
>
> On Thu, Mar 13, 2008 at 10:27 PM, David A. Black <dblack@rubypal.com> wrote:
>
> > Hi --
> >
> > On Fri, 14 Mar 2008, Demonic Software wrote:
> >
> > > Hello,
> > >
> > > Is there a way to name all the methods and attributes/ class
> > instantiated
> > > variables that have been created? For example, from the class below, I
> > > would like to get all the variables instantiated from the initialize and
> > > example methods, and then I would also like to be able to get the method
> > > names.
> > >
> > >
> > > #pseudo code, not sure if it really runs
> > > class Foo
> > > def initialize()
> > > @thestring = ''
> > > @haha = "HA HA HAA!"
> > > @var = "I am a variable"
> > > end
> > > def example(arg)
> > > @arg = arg
> > > @thestring = "#{@haha} #{@var}: #{arg}"
> > > end
> > > def thestring
> > > @thestring
> > > end
> > > end
> > >
> > >
> > > So is there a way to extract ["thestring", "var", "haha", "arg"] for the
> > > variable names and then ["initialize", "example", "thestring"] for the
> > > method names. I can see how I would do it with regular expressions and
> > > ruby2ruby, but I was wondering if there was another way of extracting
> > the
> > > information. Thanks in advance.
> >
> > You can get method and instance variable names. An "attribute" is
> > really a kind of virtual construct, composed of methods and (usually)
> > instance variables, so it doesn't appear as a separate thing when you
> > do introspective stuff on the object. The main time "attribute" exists
> > is when you create them; after that, they're just methods (though one
> > is of course free to continue to call them attributes).
> >
> > Foo.instance_methods(false) will give you all the instance methods
> > defined in Foo. (The rather cryptic "false" means: don't include
> > methods created higher in the ancestry.) To get the instance variables
> > you have to instantiate the class, run the methods that create them,
> > and then call the #instance_variables method. (Just defining methods
> > with i. vars inside them doesn't create the i. vars.)
> >
> >
> > David
> >
> > --
> > Upcoming Rails training from David A. Black and Ruby Power and Light:
> > ADVANCING WITH RAILS, April 14-17 2008, New York City
> > CORE RAILS, June 24-27 2008, London (Skills Matter)
> > See http://www.r... for details. Berlin dates coming soon!
> >
> >
>

f = Foo.new
f.public_methods(false)

... works for the instance f

Todd

Trans

3/14/2008 4:46:00 PM

0



On Mar 13, 11:27 pm, "David A. Black" <dbl...@rubypal.com> wrote:
> Hi --
>
>
>
> On Fri, 14 Mar 2008, Demonic Software wrote:
> > Hello,
>
> > Is there a way to name all the methods and attributes/ class instantiated
> > variables that have been created? For example, from the class below, I
> > would like to get all the variables instantiated from the initialize and
> > example methods, and then I would also like to be able to get the method
> > names.
>
> > #pseudo code, not sure if it really runs
> > class Foo
> > def initialize()
> > @thestring = ''
> > @haha = "HA HA HAA!"
> > @var = "I am a variable"
> > end
> > def example(arg)
> > @arg = arg
> > @thestring = "#{@haha} #{@var}: #{arg}"
> > end
> > def thestring
> > @thestring
> > end
> > end
>
> > So is there a way to extract ["thestring", "var", "haha", "arg"] for the
> > variable names and then ["initialize", "example", "thestring"] for the
> > method names. I can see how I would do it with regular expressions and
> > ruby2ruby, but I was wondering if there was another way of extracting the
> > information. Thanks in advance.
>
> You can get method and instance variable names. An "attribute" is
> really a kind of virtual construct, composed of methods and (usually)
> instance variables, so it doesn't appear as a separate thing when you
> do introspective stuff on the object. The main time "attribute" exists
> is when you create them; after that, they're just methods (though one
> is of course free to continue to call them attributes).
>
> Foo.instance_methods(false) will give you all the instance methods
> defined in Foo. (The rather cryptic "false" means: don't include
> methods created higher in the ancestry.) To get the instance variables
> you have to instantiate the class, run the methods that create them,
> and then call the #instance_variables method. (Just defining methods
> with i. vars inside them doesn't create the i. vars.)

Kind of funny. I was just thinking about this before I sat down and
read this thread.

Ruby's use of the term "attribute" is rather a misnomer. I would think
it much better if "attributes" referred to instance variables. Those
are the things that give state to objects. Attributes on the other
hand would be better off referred to as "accessors".

T.