Extend vs Refinements?

Hi guys ! I try to implement a specific serialization/communication
protocol over a TCP socket. Is it ok to do something like that ?

Extend

module MY_PROTOCOL

def read
super(‘foo’)
end

def write(…)
super(‘bar’)
end

end

socket = TCPSocket.new(…)
socket.extend(MY_PROTOCOL)

Refinements

module MY_PROTOCOL

refine IO do

def read
  super('foo')
end

def write(...)
   super('bar')
end

end

end

using MY_PROTOCOL
socket = TCPSocket.new

Which is the more idiomatic way ? Thank U for your help !

Sébastien

Sébastien Durand wrote in post #1138306:

Bonjour,

as far as I understand refinements, they are there to reduce the risk
that your monkey-patching accidentally interferes with other code, e.g.
third-party-components that you use, without your knowledge.
Or the inverse: it makes sure that your own manipulations on existing
classes will not, some day, cause weeping and gnashing of teeth for
those who use your code in a way it has never been meant to be used…

On the other hand, refinements demand an effort right there, where you
have decided that they are needed.

In other words, your choices are the following : To know the
implications of your “extension” or to never have to care about them.

Me. I do not develop libraries and my objective these days is not to
provide solutions to programmers. Personally, I choose modules as they
are convenient to use and give me all the freedom that I need.

Do not underestimate DOCUMENTATION. A technique which solves many more
problems than you may be aware of.

Pick your poison :wink:

Salut,

Michael

I have overlooked the other part of the question in my above post, but
agree with Robert.

Robert K. wrote in post #1138327:

I’d do neither.
(…)
you better create another
class which implements your protocol with appropriate methods and uses
(i.e. refers to) a Socket instance.

“Aggregation is better than inheritance”. A wrapper for the Socket is
probably the best approach.

Greetings,

Michael.

Hello ! Lol : “pick your poison !” :-))

Very instructive for me to read your comments ! Thank you very much ! I
understand now better what refinements are. Obviously, it’s not what I
need here. I will then use two classes : Connection (IO Socket wrapper)
and Client (the high-level api).

Cheers,

Sébastien

(@Michael, content de savoir que je suis pas le seul franchophone ici.
Va savoir pourquoi, avec mon nom de famille (et mon anglais moyen), on
me démasque toujours rapidement. ^^)

On Fri, Feb 28, 2014 at 6:00 AM, Sbastien D. [email protected]
wrote:

Hi guys ! I try to implement a specific serialization/communication
protocol over a TCP socket. Is it ugly to do something like that ?

I’d do neither. Both suggested approaches retain the API of class
Socket, i.e. allow the user to still use socket methods. By that they
can insert arbitrary bytes into the stream and break your protocol. As
usual with networking (-> ISO 7 layer model
OSI model - Wikipedia ) you better create another
class which implements your protocol with appropriate methods and uses
(i.e. refers to) a Socket instance.

Kind regards

robert