Composition or Module

Discussion in 'Ruby' started by Tomoyuki Kosimizu, Jan 4, 2005.

  1. 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)

    -net.ne.jp
     
    Tomoyuki Kosimizu, Jan 4, 2005
    #1
    1. Advertising

  2. Tomoyuki Kosimizu

    Bill Atkins Guest

    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
    <-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)
    >
    > -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"
     
    Bill Atkins, Jan 4, 2005
    #2
    1. Advertising

  3. > > 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
     
    Douglas Livingstone, Jan 4, 2005
    #3
  4. Tomoyuki Kosimizu

    Bill Atkins Guest

    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
    <> 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"
     
    Bill Atkins, Jan 4, 2005
    #4
  5. "Tomoyuki Kosimizu" <-net.ne.jp> schrieb im Newsbeitrag
    news:-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
     
    Robert Klemme, Jan 4, 2005
    #5
  6. 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=10&y=15)

    Just trying to clarify my own thinking.

    regards,
    Randy Kramer
     
    Ruth A. Kramer, Jan 5, 2005
    #6
  7. Tomoyuki Kosimizu

    Zach Dennis Guest

    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=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
     
    Zach Dennis, Jan 5, 2005
    #7
  8. Tomoyuki Kosimizu

    Bill Atkins Guest

    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 <> 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=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"
     
    Bill Atkins, Jan 5, 2005
    #8
  9. 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
     
    Ruth A. Kramer, Jan 5, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Schutkeker

    Need Composition Editor Tutorial

    John Schutkeker, Oct 17, 2003, in forum: Java
    Replies:
    0
    Views:
    347
    John Schutkeker
    Oct 17, 2003
  2. Rick Osborn

    Aggregation vs. composition

    Rick Osborn, Jan 23, 2004, in forum: Java
    Replies:
    11
    Views:
    57,989
    amitg_amity
    Aug 31, 2009
  3. -
    Replies:
    1
    Views:
    493
    John C. Bollinger
    Apr 11, 2005
  4. -
    Replies:
    3
    Views:
    348
  5. Roedy Green
    Replies:
    0
    Views:
    394
    Roedy Green
    Feb 4, 2006
Loading...

Share This Page