[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Composition or Module

Tomoyuki Kosimizu

1/4/2005 2:33:00 PM

8 Answers

Bill Atkins

1/4/2005 3:23:00 PM

0

Module seems more natural to me, but maybe "Representable" would be a
better name.


On Tue, 4 Jan 2005 23:33:10 +0900, Tomoyuki Kosimizu
<greentea@fa2.so-net.ne.jp> wrote:
> Hi,
>
> Let me hear your opinions, composition or module.
>
> Composition:
>
> class Representation
> def to_string(obj)
> return sprintf('%s=%s', obj.class, obj.value)
> end
> end
>
> class Domain
> def initialize
> @value = 10
> end
> attr_reader :value
>
> def to_s
> Representation.new.to_string(self)
> end
> end
>
> puts(Domain.new)
>
> or Module:
>
> module Representation
> def to_s
> return sprintf('%s=%s', self.class, @value)
> end
> end
>
> class Domain
> include Representation
>
> def initialize
> @value = 10
> end
> end
>
> puts(Domain.new)
>
> greentea@fa2.so-net.ne.jp
>
>


--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"


Douglas Livingstone

1/4/2005 3:28:00 PM

0

> > def to_s
> > return sprintf('%s=%s', self.class, @value)
> > end

> Module seems more natural to me, but maybe "Representable" would be a
> better name.

Or something like "Pretty_s" because to_s exists anyway.

Douglas


Bill Atkins

1/4/2005 3:51:00 PM

0

Well, he wants to_s so that when you run "puts Domain.new" Ruby wil
automaticall call to_s on the new domain object and output the result.

Bill

On Wed, 5 Jan 2005 00:27:46 +0900, Douglas Livingstone
<rampant@gmail.com> wrote:
> > > def to_s
> > > return sprintf('%s=%s', self.class, @value)
> > > end
>
> > Module seems more natural to me, but maybe "Representable" would be a
> > better name.
>
> Or something like "Pretty_s" because to_s exists anyway.
>
> Douglas
>
>


--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"


Robert Klemme

1/4/2005 4:08:00 PM

0


"Tomoyuki Kosimizu" <greentea@fa2.so-net.ne.jp> schrieb im Newsbeitrag
news:20050104.233447.97297001.greentea@fa2.so-net.ne.jp...
> Hi,
>
> Let me hear your opinions, composition or module.
>
> Composition:
>
> class Representation
> def to_string(obj)
> return sprintf('%s=%s', obj.class, obj.value)
> end
> end
>
> class Domain
> def initialize
> @value = 10
> end
> attr_reader :value
>
> def to_s
> Representation.new.to_string(self)

Not efficient because you create a stateless instance every time you need
the conversion to string. You could make Representation a singleton though.

> end
> end
>
> puts(Domain.new)
>
> or Module:
>
> module Representation
> def to_s
> return sprintf('%s=%s', self.class, @value)

Rather do this as it is more flexible:

return sprintf('%s=%s', self.class, self.value)

Also, you might want to add

def value() nil end

to avoid errors for classes that don't implement "value()".

> end
> end
>
> class Domain
> include Representation
>
> def initialize
> @value = 10
> end
> end
>
> puts(Domain.new)

Why do you want to separate this if you override Domain#to_s (i.e. a
standard method) anyway?

You could as well

- put the implementation into Domain#to_s and leave out Representation

- Make the conversion an instance method of the module so you don't need to
create new instances of the module all the time (for composition).

- Take the second approach (i.e. the one with the mixin) but make
Representation a base class.

Maybe there are other options but that depends on your design goal. If you
tell a bit more maybe we can come up with a more appropriate solution.

Kind regards

robert

Ruth A. Kramer

1/5/2005 2:00:00 PM

0

Bill Atkins wrote:
> Module seems more natural to me, but maybe "Representable" would be a
> better name.

I'm just a newbie/lurker, but I wonder why you suggest that, particulary
in that Representable is (so far) an adjective rather than a noun. I'm
fairly sure nouns serve better as names for objects--is it different for
modules?

(That's not to say a new usage of representable as a noun could not be
"coined". (My reference for the adjective bit is the current online
Merriam-Webster (m-w.com:
http://m-w.com/cgi-bin/dictionary?book=Dictionary&va=representable&x=1...)

Just trying to clarify my own thinking.

regards,
Randy Kramer


Zach Dennis

1/5/2005 3:01:00 PM

0

Ruth A. Kramer wrote:
> Bill Atkins wrote:
>
>>Module seems more natural to me, but maybe "Representable" would be a
>>better name.
>
>
> I'm just a newbie/lurker, but I wonder why you suggest that, particulary
> in that Representable is (so far) an adjective rather than a noun. I'm
> fairly sure nouns serve better as names for objects--is it different for
> modules?
>
> (That's not to say a new usage of representable as a noun could not be
> "coined". (My reference for the adjective bit is the current online
> Merriam-Webster (m-w.com:
> http://m-w.com/cgi-bin/dictionary?book=Dictionary&va=representable&x=1...)
>
> Just trying to clarify my own thinking.

According to your link:
"7 : to describe as having a specified character or quality "

Modules create a namespace for functionality that doesn't necessarily
constitute creating a class. With this you could say that Modules
describe a set of functionality (or characters/quality).

Modules aren't a thing, they don't describe an object, an item, a noun
by itself. They describe a set of functionality that can be used on a
*thing*. That's why they are so useful as mixins. This is also why I
agree with Bill Atkins in that Representable is a better name for a
Module, then Representation.

If you create a class MyRep which mixins module Representable you are
giving MyRep the behavior and ability to be a representation. This
behavior is best described as being Representable.

Zach


Bill Atkins

1/5/2005 4:13:00 PM

0

Modules generally add functionality to another module or class. Since
they can't be made into objects on their own, I think adjectives are
more module names than nouns. Basically, by including this module,
you're making a class able to represent itself (i.e. the class
becomes Representable). matz follows the same convention with the
Enumerable and Comparable classes in Ruby.

Bill

On Wed, 5 Jan 2005 23:00:03 +0900, Ruth A. Kramer <rhkramer@fast.net> wrote:
> Bill Atkins wrote:
> > Module seems more natural to me, but maybe "Representable" would be a
> > better name.
>
> I'm just a newbie/lurker, but I wonder why you suggest that, particulary
> in that Representable is (so far) an adjective rather than a noun. I'm
> fairly sure nouns serve better as names for objects--is it different for
> modules?
>
> (That's not to say a new usage of representable as a noun could not be
> "coined". (My reference for the adjective bit is the current online
> Merriam-Webster (m-w.com:
> http://m-w.com/cgi-bin/dictionary?book=Dictionary&va=representable&x=1...)
>
> Just trying to clarify my own thinking.
>
> regards,
> Randy Kramer
>
>


--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"


Ruth A. Kramer

1/5/2005 6:34:00 PM

0

Bill Atkins wrote:
> Modules generally add functionality to another module or class. Since
> they can't be made into objects on their own, I think adjectives are
> more module names than nouns. Basically, by including this module,
> you're making a class able to represent itself (i.e. the class
> becomes Representable). matz follows the same convention with the
> Enumerable and Comparable classes in Ruby.

Bill (and Zach),

Thanks!

Randy Kramer