Jun 13

Ruby-like Mixins in ActionScript 3

Tag: AS3, RubyDerek Wischusen @ 2:26 am

That title may be a bit of a stretch, AS3 does not support mixins in the same way as Ruby, but it is possible to define a set of methods and/or attributes in a single file and then mix those methods and attributes into your AS3 classes. Here’s how:

First create a new ActionScript file (a file, not a class) then add the methods and/or attributes that you want to mix in to your classes.

For example, I created a file called classmixin.as that looks like this:

// ActionScript file 
 
	import flash.utils.*; 
 
        // returns the name of the class 
	public function whoAmI () : String 
	{ 
		return getQualifiedClassName(this); 
	} 
 
        // returns the class object 
	public function myClass () : Class 
	{ 
		return getDefinitionByName(whoAmI()) as Class; 
	} 
 
        // returns the prototype object 
	public function proto () : Object 
	{ 
		return myClass().prototype; 
	}

Now, let’s say that you want to add these methods directly to two classes, one is controller called ContactsController and one is a model called Contact. To do this you need to use the include statement along with the relative path to file that defines the methods. For example:

package com.rxr.mixinexample.control 
{ 
	public class ContactsController 
	{ 
		include "/../classmixin/classmixin.as" 
	} 
}
package com.rxr.mixinexample.model 
{ 
	public class Contact 
	{ 
		include "/../classmixin/classmixin.as" 
	} 
}

That’s it. Now both of these classes have all of the methods defined in classmixin.as, so if you were to run the following code:

package { 
	import flash.display.Sprite; 
	import com.rxr.mixinexample.control.ContactsController; 
	import com.rxr.mixinexample.model.Contact; 
 
	public class MixinExample extends Sprite 
	{ 
		public function MixinExample() 
		{ 
			var a : ContactsController = new ContactsController(); 
			var b : Contact = new Contact(); 
 
			trace(a.whoAmI()); 
			trace(b.whoAmI()); 
 
		} 
	} 
}

you would see these trace statements in the console:

com.rxr.mixinexample.control::ContactsController
com.rxr.mixinexample.model::Contact

This mixin capability in AS3 works a bit differently than mixins in Ruby. In Ruby when you mixin a module you are really creating a reference to that module and when you call a method that was mixed in it is actually being delegated to the module. Also, in Ruby when you mixin in a module only the member methods get mixed in, though there is way to mixin statics as well, but you have to add some extra code to make this happen. This is because the module is an object itself and the static methods belong to the module, so if you want to add them to your class you need to extend the module.

In AS3 when you include a .as file, it is though you are appending that file directly to the class definition. So you are not creating a reference to a module, you are just adding the contents of the file to your class, so if you define static attributes or methods they will be added to your class along with the member attributes and methods.

Please let me know if you have any questions, or if any of this is unclear.

You can grab the source files for this example here.

4 Responses to “Ruby-like Mixins in ActionScript 3”

  1. Ruben Swieringa says:

    Interesting, though don’t you think this makes your code sloppy and harder to manage? I can’t really see myself ever using this practice, or think of a situation in which to use it..

  2. flexonrails.net » Blog Archive » Mixins in Actionscript 3 says:

    [...] a previous post I discussed how you can use the include statement to ‘mixin’ methods and properties [...]

  3. Tech Per says:

    Well, Ruben, I can think of a situation :-)

    A situation to avoid duplication, in this case. I have some properties, that need to be set in a lot of constructors in different classes, and I need to set to the same. I am unable to solve it with a superclass here. So, I define a mixin.as file with a lot of property assignments and simply include in all my constructors. Cool, as the property assignments is only at one place now. Maintainability has improved.

    I myself have my doubts about mixins with respect to maintainability as a general case. But you can do some pretty nifty stuff with it!

    And, thanks for post Derek!

  4. Aaron says:

    I can think of a situation.

    We’ve been using the .as file to “mixin” functionality to flex components. So instead of having actionscript and mxml in the same file, we’ve been using

    So that includes the actionscript file. Well, we’ve used the same script in a couple of different files. At compile time, I think it actually just adds the code into the class, then compiles, but it keeps source redundancy down. I think I’ll start using this more.

Leave a Reply

Buying levitra in australia
Cheap phentermine 37.5 without prescription
Buy female viagra without prescription
Xanax drug dosage
Cialis price canada
Buy viagra online cheap
Xanax canadian pharmacy
Where can i buy viagra cheap
Cheap generic valium
Female viagra uk
Phentermine online without a prescription
Viagra online without prescription overnight
Cod tramadol overnight
Where can i buy generic viagra
Canadian pharmacy for cialis
Generic viagra in india
Cheapest phentermine
Viagra discount coupons
Tramadol hcl 200mg
Online xanax no prescription
Prescription cialis online
Online xanax pharmacy
10mg valium
Cheap cialis generic
Valium uk delivery
2mg xanax
Viagra dosage information
Get viagra in canada
Prednisone dosages
Prednisone 10 mg
Cheap generic viagra
Valium for sale in the uk
Phentermine hcl 37.5 no prescription
Cheap levitra
Phentermine 37.5 results
Purchase cialis
Valium online no prescription
Viagra discount prices
Phentermine 37.5 lowest price
37.5mg phentermine
Phentermine 37.5mg diet pills
Viagra buy cheap
Cialis buy online
Tramadol online no prescription
Phentermine online canada
Viagra 100mg effects
50mg cialis
Buy brand name cialis
Buy viagra with no prescription
Viagra pfizer price
Buy tramadol without a prescription
Order cialis from canada
Buy viagra london
Levitra prices canada
Levitra canadian pharmacy
Viagra online sales
Order cheap phentermine
Propecia online prescription
Buy xanax online without a prescription
Where to buy phentermine 37.5 without a prescription
Valium for sale uk
Phentermine rx
Buy prednisone
Brand name viagra online without prescription
Generic viagra sales
Brand valium
Generic viagra canadian pharmacy
Cialis 10mg tablets
Discount phentermine 37.5 without prescription
Buy cialis online cheap
Xanax prescription prices
Valium 20mg
Viagra discounts
Buying generic viagra
Price propecia
Buying viagra
Canada pharmacy phentermine
Online prescriptions xanax
Phentermine 37.5 tablets
Buy phentermine direct pharmacy cheap
Propecia prices uk
Cialis medicine
Buy tramadol online canada
Online prescription phentermine
Generic tramadol no prescription
No prescription phentermine
Best viagra online
Viagra in usa
Buy prednisone dose pack
Where to buy xanax
Viagra in the philippines
100mg tramadol buy
Tramadol 200mg
Where to buy levitra
Tramadol no prescription overnight
Buy viagra in las vegas
Valium online fast delivery
Generic cialis no prescription
Generic viagra uk
Phentermine no prescription
Generic cialis information
Canadian cialis online
Buy viagra online safely
Propecia dosages
Generic cialis online canada
Phentermine 37.5 pills
Buying valium uk
Canada generic viagra
Tramadol online cheap
Discount xanax
Cialis cost
Buy viagra without prescription
Cheap tramadol overnight delivery
Brand levitra
Buy viagra uk without prescription
Prescription phentermine 37.5 mg
Tramadol rx online
Cialis to buy
Cialis online overnight
Buy viagra without a prescription
Cialis without prescription overnight
Phentermine online pharmacy
Tramadol generic
Price of viagra tablets in india
Where to buy prednisone without a prescription
Valium for sale in uk
Cheap viagra from india
Dosage of valium
Viagra 50mg side effects
Best place to buy xanax
Where to buy phentermine online
Buying cheap viagra
Viagra soft tablets
Phentermine 37.5 diet pills
Buy cheap valium online
Generic viagra cheap
Generic levitra 20mg
Brand cialis 100mg
Cialis information
Viagra without prescription
Uk viagra over the counter
Cialis buy uk
Ordering phentermine online
Where to buy phentermine 37.5 without prescription
Buy cialis canada
Order generic viagra
Levitra lowest price
Valium in uk
Cheap propecia canada
Buy xanax with prescription
Cialis
Generic cialis professional
Discount phentermine
Buy cheap tramadol online without prescription
Buying phentermine without a prescription
Cheap cialis prices
Canada phentermine 37.5
Get viagra prescription online
5mg valium
Viagra doses
Cialis online pharmacy canada
Buy viagra online uk
Generic viagra in the us
Buy viagra pills
Buy cialis generic
Drug valium
Where can i buy viagra without prescription
Tramadol no prescription next day
Generic cialis from canada
Cialis overnight shipping
Cialis 20mg tablets
Viagra online cheap
Viagra in canada without prescription
About phentermine 37.5
Buy valium roche online
Cialis pricing
Viagra online without prescription free shipping
Buy generic phentermine
Cheap propecia uk
Best price phentermine
Where to buy propecia in canada
20 mg valium
Overnight delivery viagra
Propecia online without prescription
10 mg valium effects
Buying phentermine online
Cheap online tramadol
Price of phentermine
Cheapest online tramadol
Viagra express shipping
Buy cheap tramadol without prescription
Discount xanax no prescription
Viagra prescription cost
Overnight viagra delivery
Buy cheap viagra
Viagra without rx
Buy cheap viagra pills
Order phentermine online without a prescription
Buy viagra in canada online
Discount levitra
Cialis 10mg price
Best prices for viagra
Buying levitra without prescription
Canada pharmacy viagra
Phentermine 37.5 online prescription
Cheap 100mg viagra
Buy cheap generic viagra online
Online prescription for xanax
Discount propecia
Viagra prices
Buy viagra uk
Genuine pfizer viagra
Best viagra dose
Order xanax without prescription
Purchase phentermine without a prescription
Valium online order
Viagra cost canada
Buy valium no prescription
Cialis online canada
Buy viagra without prescription online
Order levitra online
Cheap cialis viagra
Discount generic viagra
Free samples of viagra
Fast shipping viagra

Buy tramadol hcl
Purchase viagra canada
Buy real viagra online without prescription
Phentermine in the uk
Where to buy xanax without prescription
Cialis uk online
Tramadol hcl 50mg side effects
Cialis generic 5mg
Discount tramadol
Order xanax online without prescription
Best way to take tramadol
Generic viagra professional
Viagra prescriptions
Where can i buy tramadol
Propecia buy canada
Cheap levitra pills
50 mg viagra online
Cialis uk supplier
Order viagra overnight
Prescription xanax
Cheap xanax no prescription
Buy cialis brand
Cialis professional generic
Cheap tramadol online overnight delivery