Skip to content

Instantly share code, notes, and snippets.

@DouglasAllen
DouglasAllen / webrick_and_erb.rb
Last active August 29, 2015 14:27 — forked from ramn/webrick_and_erb.rb
Simple ruby webserver
require 'webrick'
require 'erb'
template = <<TEMPLATE
<html>
<head>
<title>Ruby as PHP</title>
</head>
<body>
<h1>Loop</h1>
@DouglasAllen
DouglasAllen / gist:1a30612c975820d4316e
Last active August 29, 2015 14:27 — forked from jamiehodge/gist:1327195
Warden and Sinatra example
require 'sinatra/base'
require 'rack/flash'
require 'warden'
require 'slim'
require 'sequel'
require 'sqlite3'
DB = Sequel.sqlite
DB.create_table :users do
primary_key :id
Warden::Manager.serialize_into_session{|user| user.id }
Warden::Manager.serialize_from_session{|id| User.get(id) }
Warden::Manager.before_failure do |env,opts|
# Sinatra is very sensitive to the request method
# since authentication could fail on any type of method, we need
# to set it for the failure app so it is routed to the correct block
env['REQUEST_METHOD'] = "POST"
end
# File lib/rack/sendfile.rb, line 112
def call(env)
status, headers, body = @app.call(env)
if body.respond_to?(:to_path)
case type = variation(env)
when 'X-Accel-Redirect'
path = F.expand_path(body.to_path)
if url = map_accel_path(env, path)
class Eot
attr_accessor :date, :lat, :lng, :attributes
def initialize attributes
@attributes = []
attributes.each_key do |k|
self.send("#{k}=", attributes[k])
@attributes << attributes[k]
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
#run DemoApp::Application
app = Rack::Builder.new do
# Create a SQLite3 db file from a SQL structure file.
require "yaml"
require "active_record"
if $0 == $PROGRAM_NAME
include ActiveRecord::Tasks
file = "./config/database.yml"
config = YAML.load_file(file)
module HelloWorld
def show
puts "Hello World!"
end
end
puts "#{HelloWorld} is a module class."
puts "See? #{HelloWorld.class}"
@DouglasAllen
DouglasAllen / irb_session.rb
Last active August 29, 2015 14:13
Classes are Objects
self
#=> main
self.class
#=> Object
self.object_id
#=> -595331298
o = Object.new
#=> #<Object:0xb940af38>
def titleize(str);str.split(" ").map {|w| w.capitalize}.join(" ");end
titleize("x-men: the sky is the limit")