Attr_accessor

Hi
* is there an attr_accessor for class methods?

* how can I use attr_accessor in initialize() ?
(The args to attr_accessor are given when calling it)

* How can I see (with ruby-code) from which object/module the attr_accessor method comes (is implemented in) ?

thank yoiu
Opti

* is there an attr_accessor for class methods?

Depends what you mean. attr_accessor works with instance attributes. What do you consider a class attribute?

You can certainly do this:

    class Foo

      class << self
        attr_accessor :bar
      end

    end

But what you will be getting and setting is the instance attribute of the eigenclass(?) and certainly not a @@bar class attribute. (At the Foo instance level you would access it using `myfoo.class.bar`.)

On the other hand, since class attributes are rather compromised, this might arguably be better...

On the third hand, however -- any complex structure on the class itself should really live in a separate object; it's considered by some to be a code smell. So maybe a rethink might be in order.

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt;

* is there an attr_accessor for class methods?

This question doesn't make much sense to me...
attr_accessor is used with instance variables.
Instance variables cannot be accessed from class methods.

What is the "attr_accessor for class methods"
supposed to do?

I suspect you are not very clear about basic concepts
(classes, instances, instance variables, constants,
instance vs. class methods, ...) and would suggest you
read some introductory tutorials on these topics and
how to create classes in Ruby.

* how can I use attr_accessor in initialize() ?
(The args to attr_accessor are given when calling it)

Are you sure you need to dynamically create instance variables?
Maybe you could simply use a hash internally (@attributes, @fields),
and use the arguments in initialize as hash keys.
You could define # and #= methods to access those fields.

BTW: please use more descriptive subject lines.

Regards,
Marcus

···

Am 09.01.2017 um 11:30 schrieb Die Optimisten:

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A