Evented Ruby, yo

 

Kalon Hinds

http://github.com/hkal

@_hkal

 
$('.btn').click(function() {
    $(this).remove();
});
 

In the beginning,

there were batch processing systems

  • non-interactive jobs
  • jobs could be stored and executed later
  • once a job began it continued until completion or an error occurred

However, as systems became larger and more complex people realized that...

 

Structured Design

Edward Yourdon, Larry L. Constantine

"Design Patterns" → Object-oriented programming

"Structured Design" → Structured programming

Transaction Analysis

Transaction Center

  • get transactions
  • analyze each transaction to determine its type
  • dispatch on type of transaction
  • complete the processing of each transaction
 
loop do
    # wait for user input
    input = gets.chomp
    
    if input == "1"
        # do whatever pressing "1" does
    elsif input == "k"
        # do whatever pressing "k" does
    elsif input == "q"
        # clean-up and exit application
    else
        # raise exception for unhandled event type or ignore
    end
end

Yeah, that's right, pretty much everyone has written a transaction center

Let's modernize and simplify

  • stream of data called events (transactions)
  • a dispatcher (transaction center)
  • a set of subroutines called event handlers

Event-driven Programming

  • Application control flow is determined by events
  • has an event loop and various event handlers
 

but, what are events anyway?

 

Events are...

 

a significant change in state

 

Shoes

  • GUI toolkit
  • a Ruby DSL built upon jruby and SWT
  • Originally created by _why
Shoes.app(title: "See this? It's a shoes app") do
  # make GUI things in here
  # P.S... nobody knows shoes
end
 

Observer Pattern

  • One object notifies a set of third-party objects
  • One-to-many relationship
  • Used in MVC
    • Decouples models from views
    • Model → Observerable; View  → Observer
 

Reactor Pattern

  • Calls handlers upon events
  • Allows events from multiple sources
  • Single threaded process
  • Non-block IO

Constraints

  • Synchronous event dispatching
  • Code must be written in a non-blocking manner

http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf

http://www.cs.vu.nl/~eliens/online/oo/I/2/events.html

http://www.slideshare.net/autonomous/ruby-concurrency-and-eventmachine

http://en.wikipedia.org/wiki/Event-driven_programming

http://iweb.dl.sourceforge.net/project/eventdrivenpgm/ event_driven_programming.pdf


Evented Ruby, yo

By hkal

Evented Ruby, yo

Event Driven Programming in Ruby

  • 1,616