Evented Ruby, yo


Kalon Hinds



$('.btn').click(function() {

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
        # raise exception for unhandled event type or ignore

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



  • 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

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


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





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

Evented Ruby, yo

By hkal

Evented Ruby, yo

Event Driven Programming in Ruby

  • 872
Loading comments...

More from hkal