subramaniam.taruvai-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Mar-26 20:15 UTC
How to do multiple inheritance with mixins in 1.6?
Could someone show me a code snippet for mixing in mutiple mixins into a class? Say I have the Vulnerable and Dangerous mixins and I want to mix it into a Person class. My problem is I can do var Person = Class.create(Vulnerable, { initialize: function() { this.health = 100; this.dead = false; } }); but how do I mixin Dangerous? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-Mar-26 20:46 UTC
Re: How to do multiple inheritance with mixins in 1.6?
Well, you''re going to run into trouble if both the classes you are trying to mixin to you Person class are both implementing the same method names, but assuming that is not the case you can use Object.extend to mixin another set of methods when you''re creating the class. This is pretty crazy and it scares me, but I guess it works... var Foo = { bar: function(){ console.log("I am the bar method on the Foo object"); } }; var Hello = { world: function(){ console.log("I am the world method on the Hello object"); } }; var FooHello = Class.create(Foo, Object.extend({ initialize: function(){ } }, Hello)); var myInstance = new FooHello(); myInstance.bar(); myInstance.world(); -justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-Mar-26 20:50 UTC
Re: How to do multiple inheritance with mixins in 1.6?
I should follow up that when I said "if both the classes" I really meant to say objects. I don''t even want to know what happens when you start mixing together multiple classes into a child class (which all would have an initialize method) and then try to instantiate a new hybrid object. Don''t do it. -justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Just pass it before "initialization" object (the one that defines instance methods and constructor). You could pass as many mixins as you want this way: var Vulnerable = { getShot: function() { this.health -= 20; }, } var Dangerous = { shoot: function(other) { other.getShot(); } } var Person = Class.create( Vulnerable, Dangerous, { initialize: function() { this.health = 100; this.dead = false; } }) var attacker = new Person; var victim = new Person; attacker.shoot(victim); victim.health; // => 80 attacker.shoot(victim); victim.health; // => 60 // etc. Best, kangax On Mar 26, 4:15 pm, subramaniam.taru...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Could someone show me a code snippet for mixing in mutiple mixins into > a class? Say I have the Vulnerable and Dangerous mixins and I want to > mix it into a Person class. My problem is I can do > var Person = Class.create(Vulnerable, { > initialize: function() { > this.health = 100; > this.dead = false; > }}); > > but how do I mixin Dangerous?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-Mar-27 01:21 UTC
Re: How to do multiple inheritance with mixins in 1.6?
On Wed, Mar 26, 2008 at 7:16 PM, kangax <kangax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> var Person = Class.create( Vulnerable, Dangerous, ...Nice! I tried passing an array of modules as the first argument but didn''t think to try like that. Way nicer. Guess I should have looked at the source code first. :) -justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---