Mariusz Pekala
5/11/2007 8:46:00 AM
On 2007-05-11 06:58:48 +0900 (Fri, May), Robert Dober wrote:
> On 5/10/07, Jesse Merriman <jesse.d.merriman@gmail.com> wrote:
> >On Thursday 10 May 2007 06:00, Bernd wrote:
> >> Hi,
> >> I wrote a code like this.
> >>
> >> class ABC
> >> attr_accesor :first, :second
> >> end
> >>
> >> when I wrote the following:
> >>
> >> x = ABC.new
> >> x.first = "test"
> >> puts x.instance_variables
> >>
> >> the output was only
> >>
> >> @first
> >
> >You could deduce them by looking for methods that end in an equals:
> Hmm that might not be a good idea, a method ending in equals does not
> induce the creation of an instance variable with that name.
> One could however put attr on steroids by creating a list of
> attributes for the class.
> Look at this quick and dirty hack do see if this could be helpfull for you
>
> class Module
> alias_method :orig_attreader, :attr_reader
> def attr_reader *symbols
> @__atts__ ||= superclass.attributes.dup
> @__atts__ += symbols
> @__atts__.uniq!
> orig_attreader *symbols
> end
>
> def attributes
> @__atts__ || []
> end
> end
>
<snip>
..or, since the original problem was that no instance variable has been
created, just create it (in quick and dirty way):
class Module
alias_method :orig_attr, :attr
def attr(symbol, writable = false)
orig_attr symbol, writable
send symbol
end
...
--
No virus found in this outgoing message.
Checked by 'grep -i virus $MESSAGE'
Trust me.