Table of Contents

  1. Simple Definition Test
  2. Super Test
  3. Database
    1. Active Record
  4. User Interaction
    1. Highline
Simple definition test
def test
   puts "this is a test"
end

Super Test

class YammDB < Hash
   def initialize(opts={})
      puts "making YammDB #{opts.inspect}"
      super
   end
end
class Sims < YammDB
   def initialize(opts={})
      puts "making Sims with opts: #{opts.inspect}"
      super(opts)
   end
end
   
Sims.new(:blah)
Sims.new()

Database

Active Record

#!/usr/bin/ruby
require 'rubygems'
require 'sqlite3'
require 'activerecord'
   
# connect to database.  This will create one if it doesn't exist
MY_DB_NAME = File.dirname(__FILE__) + "/my.db"
MY_DB = SQLite3::Database.new(MY_DB_NAME)
   
# get active record set up
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => MY_DB_NAME)
ActiveRecord::Base.logger = Logger.new(File.open("db.log","a"))
ActiveRecord::Base.colorize_logging = false
#ActiveRecord::Base.logger = Logger.new(STDERR)
   
# create your AR class
class Routing < ActiveRecord::Base
   has_and_belongs_to_many :destinations
   has_and_belongs_to_many :sources
end
class Source < ActiveRecord::Base
   has_many :routings
   has_many :subsystems
end
class Destinations < ActiveRecord::Base
   has_many :routings
   has_many :subsystems
end
class Subsystem < ActiveRecord::Base
   has_many :destinations
   has_many :sources
end
   
class CreateTable01 < ActiveRecord::Migration
   def self.up
      create_table :routings do |t|
         t.column :name, :string, :null => false
         t.column :description, :string
         t.column :xml_file, :string, :null => false
      end
      create_table :subsystems do |t|
         t.column :name, :integer
         t.column :version, :integer
      end
      create_table :sources do |t|
         t.column :subsystem_id, :integer
      end
      create_table :destinations do |t|
         t.column :subsystem_id, :integer
      end
      create_table :sources_routings, id => false do |t|
         t.column :routing_id, :integer
         t.column :subsystem_id, :integer
      end
      create_table :destinations_routings, id => false do |t|
         t.column :subsystem_id, :integer
      end
   end
      
   def self.down
      drop_table :routings
      drop_table :sources
      drop_table :destinations
      drop_table :sources_routings
      drop_table :destinations_routings
      drop_table :subsystems
   end
end
   
      
#CreateTable01.migrate(:down)
#CreateTable01.migrate(:up)
   
r = Routing.create(:name => 'test', :description => 'test routing', :xml_file => 'test.xml')
r = Routing.find_by_name('test')
   
puts r.name
   

User interaction

Highline

#!/usr/bin/ruby
require 'rubygems'
require 'highline'
   
      
@hl = HighLine.new
   
@hl.choose do |menu|
   menu.prompt = "Please choose your favorite programming language?  "
      
   menu.choice(:ruby) { @hl.say("Good choice!") }
   menu.choices(:python, :perl) { @hl.say("Not from around here, are you?") }
end