--e89a8fb20116ab63c604bfa7091e
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Wed, May 9, 2012 at 6:36 AM, Iñáki Baz Castillo <ibc / aliax.net> wrote:

> Hi, simple code:
>
> ----------------------------
>  module M
>    def hello
>      "HELLO"
>    end
>  end
>
>  class A
>  end
>
>
>  klass = A
> ----------------------------
>
> Now I want to make an instance of klass (so an instance of A) but also
> need that klass includes module M.
> Note however that I cannot change class A definition since klass is
> given in runtime by the user.
>
> NOTE: It must be efficient, I don't want to use eval and so.
>
> Thanks a lot.
>
>
Since we don't want to change A, use a subclass. It will have all the
behaviour of A, but you can include modules and so forth while leaving M in
pristine condition.

klass = Class.new(A) { include M }
klass.new.hello # => "HELLO"
A.new.respond_to? :hello # => false


Alternatively, extend your instances:

klass = A
instance = klass.new
instance.extend M
instance.hello # => "HELLO"
A.new.respond_to? :hello # => false

Extending works by opening up the singleton class and including within it.
This means the instance behaves as if A had M included, even though it
doesn't.

--e89a8fb20116ab63c604bfa7091e
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div class="gmail_quote">On Wed, May 9, 2012 at 6:36 AM, Iñáki Baz Castillo <span dir="ltr">&lt;<a href="mailto:ibc / aliax.net" target="_blank">ibc / aliax.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote"tyle="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi, simple code:<br>
<br>
----------------------------<br>
  ¨Âïäõìͼâò  ¨Âåæ èåììï¼âò quot;HELLO&quot;<br>
  ¨Âîä¼âò¾
  ¨Âîä¼âò¾
<br>
  ¨Âìáóó Á¼âò  ¨Âîä¼âò¾
<br>
<br>
  ¨Âìáóó Á¼âò----------------------------<br>
<br>
Now I want to make an instance of klass (so an instance of A) but also<br>
need that klass includes module M.<br>
Note however that I cannot change class A definition since klass is<br>
given in runtime by the user.<br>
<br>
NOTE: It must be efficient, I don&#39;t want to use eval and so.<br>
<br>
Thanks a lot.<br><br></blockquote><div><br></div><div>Since we don&#39;t want to change A, use a subclass. It will have all the behaviour of A, but you can include modules and so forth while leaving M in pristine condition.</div>
<div><br></div><div><div>klass = Class.new(A) { include M }/div><div>klass.new.hello # =&gt; &quot;HELLO&quot;</div><div>A.new.respond_to? :hello # =&gt; false</div></div><div><br></div><div><br></div><div>Alternatively, extend your instances:</div>
<div><div><br></div><div>klass = A</div><div>instance = klass.new</div><div>instance.extend M</div><div>instance.hello # =&gt; &quot;HELLO&quot;</div><div>A.new.respond_to? :hello # =&gt; false</div></div><div><br></div><div>
Extending works by opening up the singleton class and including within it. This means the instance behaves as if A had M included, even though it doesn&#39;t.</div></div>

--e89a8fb20116ab63c604bfa7091e--