Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing mixins vs calling a method directly
    primarykey
    data
    text
    <p>I'd like to know when is the best time to use mixins vs calling a method directly.</p> <p>For example, consider HTTParty. On one of its examples <a href="https://github.com/jnunemaker/httparty/blob/master/examples/basic.rb" rel="nofollow">https://github.com/jnunemaker/httparty/blob/master/examples/basic.rb</a></p> <p>you can use HTTParty.get('http://twitter.com/statuses/public_timeline.json') or you can create a class that includes HTTParty and then use it as you would calling HTTParty itself.</p> <p>What's the difference with me just creating something like this:</p> <pre><code>class Partay @base_uri = 'http://localhost:3000' def self.post(endpoint, options) HTTParty.post(@base_uri + endpoint, options) end end </code></pre> <p>versus the given example:</p> <pre><code>class Partay include HTTParty base_uri 'http://localhost:3000' end </code></pre> <p>True that in such a trivial example perhaps using the include would save more characters, but I'd imagine on a much more complicated class it doesn't really make a difference.</p> <p>A few clarificatory questions: Would this be related to a composition vs aggregation argument? Is there any design or architecture best practice regarding this? Should I treat mixins as some sort of inheritance and use them as such (inheritance if it's an is-a relationship, composition if it's a has-a relationship, etc.)? Should I only mixin a module if it was intended to be done so (because docs say it expects certain methods from you, like Enumerable) or is it just good practice to do so? Would using a mixin mean a tighter coupling between the module and my class (and is it relatively good or bad in general)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload