Ruby mixin

Like many other programmers I wish I had started learning Ruby when it was created, all the more so for the demand for broad programming skills now. I started using NetBeans 6.1 which made JRuby programming easier. I started to look at some example code written in other languages and write equivalent Ruby code. If you have good programming experience ( Updated : Hope it is not the Kruger-Dunning effect :-)then you can start with fairly complex code when learning a new language.

The example is JavaScript event code that can be mixed-in with other code. Actually I came across many powerful features of JavaScript when I programmed using RhinoScript recently.

I hope to update the code but as of now I have the skeleton below. I wouldn’t be able to explain everything because this is my first serious piece of Ruby code. I will try to use this “mixin” code along with other code.

    Updated : Used a module. Exploring how to write proper test code.


module EventMixinModule

  EventManager   = { "notify" => proc {
                                    puts "notify"
                                    if ( nil == defined?($listeners) )
                                      puts "No listeners registered"
                                    puts $listeners
                     "listen" => proc {
                                    puts "listen"
                                    $listeners = listen || [];
  puts EventManager["listen"].call( "listener" )
  #puts EventManager["notify"].call


require "event_mixin_module"

class DomainObject
  include EventMixinModule
  def initialize
    puts "DomainObject"
  def setvalue
  puts "setvalue"



require "domain_object"

class DomainObjectTest
  def initialize
  domainobject =

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: