[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ruby idiom for attribute definition?

Corey

11/11/2004 4:28:00 AM


Hey!

What do you guys do when you have a class with lots of simple attributes?

Do you use attr_reader, and attr_writer shortcuts with each of the attributes?
Or do you use a singe method (say, object.set('attr', 'val') for all of them?
Or do you manually create each of the reader/writer methods?

Also, how many attributes does it take to be considered "alot"?

( I've got a really small class going, with about 9 or 10 simple attributes -
each of them readable and writeable. )


Thanks!

Beers,

Corey

--

Misery is the river of the world.
Everybody row.
- Tom Waits


18 Answers

Dave Thomas

11/11/2004 4:41:00 AM

0


On Nov 10, 2004, at 22:27, Corey wrote:

> ( I've got a really small class going, with about 9 or 10 simple
> attributes -
> each of them readable and writeable. )

Have you considered using Struct for this? It creates the initialize
method for you, as well as all the attribute accessors. You can extend
the class it creates, but you have to access the attributes via
accessors, and not via instance variables

irb(main):001:0> Thing = Struct.new(:name, :age)
=> Thing
irb(main):002:0> class Thing
irb(main):003:1> def desc
irb(main):004:2> "Person #{self.name} is #{self.age} years old"
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> t = Thing.new("Walter", 22)
=> #<struct Thing name="Walter", age=22>
irb(main):008:0> t.desc
=> "Person Walter is 22 years old"

You could also say

class Thing < Struct.new(...)


Cheers

Dave



Neil Stevens

11/11/2004 5:55:00 AM

0

On Thu, 11 Nov 2004 13:27:42 +0900, Corey wrote:

> Hey!
>
> What do you guys do when you have a class with lots of simple attributes?
>
> Do you use attr_reader, and attr_writer shortcuts with each of the attributes?
> Or do you use a singe method (say, object.set('attr', 'val') for all of them?
> Or do you manually create each of the reader/writer methods?

Try using attr_accessor, which makes an attribute readable and writable.

--
Neil Stevens - neil@hakubi.us
"The world is a dangerous place to live; not because of the people who
are evil, but because of the people who don't do anything about it."
-- Albert Einstein(?)

Jim Freeze

11/11/2004 6:53:00 PM

0

* Dave Thomas <dave@pragprog.com> [2004-11-11 13:40:43 +0900]:

> You could also say
>
> class Thing < Struct.new(...)
>

This got me thinking, so I tried the following:

class A < Struct.new(:a)
def peek
puts @a
end
end

A.new.peek

Turns out that this does not work since @a is NOT defined.
Problem is, I can't see any instance variables defined
inside a struct. Where does it keep the data?

--
Jim Freeze


Dave Thomas

11/11/2004 7:07:00 PM

0


On Nov 11, 2004, at 12:52, jim@freeze.org wrote:

> * Dave Thomas <dave@pragprog.com> [2004-11-11 13:40:43 +0900]:
>
>> You could also say
>>
>> class Thing < Struct.new(...)
>>
>
> This got me thinking, so I tried the following:
>
> class A < Struct.new(:a)
> def peek
> puts @a
> end
> end
>
> A.new.peek
>
> Turns out that this does not work since @a is NOT defined.
> Problem is, I can't see any instance variables defined
> inside a struct. Where does it keep the data?

That's why I said you have to use the accessor methods. Structs cheat.

Matz: we discussed this before, but it would be really nice if Struct
set up the IVs too: that way this idiom would be more generally useful.


Cheers

Dave



Corey

11/11/2004 7:17:00 PM

0

On Wednesday 10 November 2004 09:40 pm, Dave Thomas wrote:
> On Nov 10, 2004, at 22:27, Corey wrote:
> > ( I've got a really small class going, with about 9 or 10 simple
> > attributes -
> > each of them readable and writeable. )
>
> Have you considered using Struct for this? It creates the initialize
> method for you, as well as all the attribute accessors.
<snip>
> class Thing < Struct.new(...)
>

I can see the usefullness in this, thanks for the heads-up - but I
can see that if I had a lot of attributes, it would seem somewhat
awkward:

class Thing < Struct.new (:attr1, :attr2, :attr3, :attr4, :attr5,
:attr6,:attr7, :attr8, :attr9, :attr10 )
# do stuff
end

Besides, I just get a weird feeling that this Struct approac is kinda
odd - to tricky or something - as I'm learning ruby, I'm trying to stick
with the most common idioms and oop practices.

I guess it's just a matter of personal aesthetics? :

class Thing
attr_accessor :attr1, :attr2, :attr3, :attr4, :attr5,
:attr6,:attr7, :attr8, :attr9, :attr10
# do stuff
end


Thanks,

Corey

--

"Good judgment comes from experience.
Experience comes from bad judgment."
- Jim Horning


Corey

11/11/2004 7:19:00 PM

0

On Wednesday 10 November 2004 10:53 pm, Neil Stevens wrote:
> On Thu, 11 Nov 2004 13:27:42 +0900, Corey wrote:
> > What do you guys do when you have a class with lots of simple attributes?
> >
<snip>
> Try using attr_accessor, which makes an attribute readable and writable.
>

Cool - I hadn't run into that in the Pickaxe yet; I think I like it a little
better that the Struct suggestion ( thanks Dave ) for my particular
purposes in this case.

Thanks for the clues!

Cheers,

Corey

--

"Never believe anything until it is officially denied."
- Claud Cockburn


James Gray

11/11/2004 7:30:00 PM

0

On Nov 11, 2004, at 1:18 PM, Corey wrote:

> Cool - I hadn't run into that in the Pickaxe yet; I think I like it a
> little
> better that the Struct suggestion ( thanks Dave ) for my particular
> purposes in this case.

For what it's worth, I think the Struct is better OO design. I'm
firmly in the "Ask for help, not information." camp of OO programmers,
so I try to avoid "getters/setters" whenever possible.

A Struct on the other hand is just a record holder to my mind, so I
have much less problem with using it in a case like this.

Not trying to start a Holy War here, just tossing in my two cents.

James Edward Gray II



Dave Thomas

11/11/2004 7:46:00 PM

0


On Nov 11, 2004, at 13:29, James Edward Gray II wrote:
>
> For what it's worth, I think the Struct is better OO design. I'm
> firmly in the "Ask for help, not information." camp of OO programmers,
> so I try to avoid "getters/setters" whenever possible.
>
> A Struct on the other hand is just a record holder to my mind, so I
> have much less problem with using it in a case like this.

Exactly - that's what made me suggest it. A class with a large number
of attr_accessors is not really an encapsulated class, so it felt to me
that a Struct would be more appropriate.

I almost suggested using a Hash... :)


Cheers

Dave



Corey

11/11/2004 8:05:00 PM

0

On Thursday 11 November 2004 12:46 pm, Dave Thomas wrote:

> On Nov 11, 2004, at 13:29, James Edward Gray II wrote:
> > For what it's worth, I think the Struct is better OO design. I'm
> > firmly in the "Ask for help, not information." camp of OO programmers,
> > so I try to avoid "getters/setters" whenever possible.
> >

And from the perspective a new ruby user, I'm in the "ask for help _and_
information" camp - so these explanations are really beneficial, thanks!


> > A Struct on the other hand is just a record holder to my mind, so I
> > have much less problem with using it in a case like this.
>
> Exactly - that's what made me suggest it. A class with a large number
> of attr_accessors is not really an encapsulated class, so it felt to me
> that a Struct would be more appropriate.
>
> I almost suggested using a Hash... :)
>

Heh - I actualy started off with a Hash, but I thought that was just my
old perl habits trying break through... <grin>

I'm in the midst of writing another question, which looks to be related
to all this - ( perhaps I should be using a struct or hash after all ) -
hopefully you guys can point me in the right direction.



--

"To a mind that is still, the whole universe surrenders."
- Chuang Tzu


Jim Freeze

11/11/2004 8:21:00 PM

0

* Dave Thomas <dave@pragprog.com> [2004-11-12 04:46:04 +0900]:

> Exactly - that's what made me suggest it. A class with a large number
> of attr_accessors is not really an encapsulated class, so it felt to me
> that a Struct would be more appropriate.

Below is an example that speaks to your comment about structs not being
generally useful for inheritance:

S = Struct.new(:a, :b)

class C < S
attr_accessor :c, :d

def do_something_clever
"#{a} : #{b} | #{@c} : #{@d}"
end
end

The problem is that I have to know which IV came from C
and which came from Struct. I suppose that I could use
the getter/setter methods for all IV's, but I thought
that @c was avoiding a function call that 'c' was making,
and I would like to avoid that.

Dave, earlier you suggested that Structs cheat. Well,
I suppose I could try to out cheat the struct with:

class C < S
attr_accessor :a, :b, :c, :d
def ...
end
end

Now I get all the benefits of Struct for :a and :b,
but the problem is that I am violating DRY.

--
Jim Freeze