Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Composition or Module

Reply
Thread Tools

Composition or Module

 
 
Tomoyuki Kosimizu
Guest
Posts: n/a
 
      01-04-2005
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)

http://www.velocityreviews.com/forums/(E-Mail Removed)-net.ne.jp


 
Reply With Quote
 
 
 
 
Bill Atkins
Guest
Posts: n/a
 
      01-04-2005
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
<(E-Mail Removed)-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)
>
> (E-Mail Removed)-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"


 
Reply With Quote
 
 
 
 
Douglas Livingstone
Guest
Posts: n/a
 
      01-04-2005
> > 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


 
Reply With Quote
 
Bill Atkins
Guest
Posts: n/a
 
      01-04-2005
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
<(E-Mail Removed)> 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"


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-04-2005

"Tomoyuki Kosimizu" <(E-Mail Removed)-net.ne.jp> schrieb im Newsbeitrag
news:(E-Mail Removed)-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

 
Reply With Quote
 
Ruth A. Kramer
Guest
Posts: n/a
 
      01-05-2005
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?bo...able&x=10&y=15)

Just trying to clarify my own thinking.

regards,
Randy Kramer


 
Reply With Quote
 
Zach Dennis
Guest
Posts: n/a
 
      01-05-2005
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?bo...able&x=10&y=15)
>
> 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


 
Reply With Quote
 
Bill Atkins
Guest
Posts: n/a
 
      01-05-2005
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 <(E-Mail Removed)> 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?bo...able&x=10&y=15)
>
> 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"


 
Reply With Quote
 
Ruth A. Kramer
Guest
Posts: n/a
 
      01-05-2005
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


 
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
Aggregation vs. composition Rick Osborn Java 11 08-31-2009 08:45 AM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
Can Tbird save a mail composition - periodically? default Firefox 5 10-16-2005 02:50 PM
Mozilla Mail composition spaces on screen quincey cavenaugh Firefox 8 02-14-2004 06:01 PM
Need Composition Editor Tutorial John Schutkeker Java 0 10-17-2003 03:40 PM



Advertisments