[LRUG] Understanding Ruby Tempfile documentation

Stephen Best bestie at gmail.com
Wed Jan 27 03:31:00 PST 2016


On 26 January 2016 at 13:10, Craig R Webster <craig at xeriom.net> wrote:

> If you did it this way Tempfile would no longer have the same interface as
> File. The constructor signature has changed so I think you’d be violating
> the Liskov substitution principle. If Tempfile IS-A File then I should be
> able to use it in place of File in any code that uses File.


What you're describing here isn't a violation of the Liskov substitution
principle because an instance of Tempfile still substitutes for an instance
of File, the signature of the constructor is not relevant to code that
interacts with instances of those classes after instantiation.

If however you had an object of class FileFactory, with instance method
`#create`, that created instances of File and you wanted to subclass that
(TempFileFactory) to produce instances of TempFile then it would be useful
if you could substitute one for the other. Again Liskov's principle
wouldn't apply to the constructors of those factory objects, but only to
their `#create` method.

Inheritance is, in my experience, over used due its convenience so I
usually find myself arguing against it.

Composition brings the cost of extra code necessary to compose
collaborating objects together but results in those objects each being
simpler, more flexible and reusable.

Inheritance on the other hand is very convenient and produces a static
structure that is often appealing. The downside is its tendency to result
in complex objects that are difficult to work with. ActiveRecord for
example uses multiple inheritance and exposes 400+ instance methods to a
subclass before its own methods are added.


Bestie.

theaudaciouscodeexperiment.com
github.com/bestie
@thebestie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20160127/4dee4b74/attachment.html>


More information about the Chat mailing list