Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Where and how is defined "attr_accessor"?

Reply
Thread Tools

Where and how is defined "attr_accessor"?

 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      04-29-2008
Hi, I've done some custom "field_accessor" (similar to "attr_accessor" but=
=20
with some neccessary difference). It works perfectly if I=20
defined "field_accessor" in the same class:

class Header
def self.field_accessor(name)
module_eval %{ def #{name}() .... end }
end
end

class From < Header
field_accessor :user, :domain
end


But I'd like to have field_accessor definition out of "Header" class, maybe=
in=20
a module but don't get it working. If I do:

module FieldAccessor
module_eval %{ def #{name}() .... end }
end

class From
include FieldAccessor
field_accessor :user, :domain
end


Then I get:
NoMethodError: undefined method `field_accessor' for From:Class


Later I've tryed with the following and it seems to work:

module FieldAccessor
module_eval %{ def #{name}() .... end }
# It also works with "class_eval".
end

class From
extend FieldAccessor
field_accessor :user, :domain
end



So, while I was writting this mail I found the solution, but would like to=
=20
know if it's the correct way. Thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      04-29-2008
El Mi=C3=A9rcoles, 30 de Abril de 2008, David A. Black escribi=C3=B3:
> The last two definitions of FieldAccessor look wrong; you haven't
> defined the field_accessor method. But assuming that you correct that,
> then yes, extending your class is the best way.


Yes, sorry, what I did is:

module FieldAccessor
def self.field_accessor(name)
module_eval %{ def #{name}() .... end }
# It also works with "class_eval".
end
end

class From
extend FieldAccessor
field_accessor :user, :domain
end



> To answer the question in your subject line: attr_accessor is an
> instance method of Module. That's why all modules and classes can call
> it. You could add field_accessor to Module, but extend is cleaner.


Nice to know, so I was in the good way

Thanks a lot for your help.



=2D-=20
I=C3=B1aki Baz Castillo

 
Reply With Quote
 
 
 
 
Ken Bloom
Guest
Posts: n/a
 
      05-01-2008
On Tue, 29 Apr 2008 18:26:39 -0500, Iñaki Baz Castillo wrote:

> El Miércoles, 30 de Abril de 2008, David A. Black escribió:
>> The last two definitions of FieldAccessor look wrong; you haven't
>> defined the field_accessor method. But assuming that you correct that,
>> then yes, extending your class is the best way.


Another reasonable place is in class Module. See Ruby Quiz #67:
Metakoans, which involved creating an attr_accessor like method.
http://www.rubyquiz.com/quiz67.html

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
short form for (defined $x and defined $y and ... )? Markus Dehmann Perl Misc 19 03-27-2006 08:32 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57