Skip to content

Instantly share code, notes, and snippets.

@mayfer
mayfer / algorithms.rb
Created April 20, 2015 20:14
algorithms and Data structures examples
def find_index(numbers, number)
numbers.each_with_index do |n, i|
if n == number
return i
end
end
return nil
end
@mayfer
mayfer / http.rb
Created April 17, 2015 21:57
Demonstrating HTTP concepts with sinatra
require 'sinatra'
post '/' do
response.set_cookie("username", params[:username])
redirect '/'
end
get '/logout' do
response.set_cookie("username", value: "test", expires: Time.now )
@mayfer
mayfer / orm.rb
Created April 14, 2015 17:54
Generic ORM example, that works for any class that matches a table.
class ORM
def save
if defined? @id
sql_update
else
sql_insert
end
end
@mayfer
mayfer / venue.rb
Created April 14, 2015 17:53
Simple ORM livecoding example
require 'pg'
$conn = PG::Connection.open(:dbname => 'lighthouse')
class Venue
attr_accessor :name, :capacity, :id
def initialize(name, capacity)
@id = nil
@name = name
@mayfer
mayfer / gamblerat.html
Created April 7, 2015 18:31
jQuery examples (event propagation, dom manipulation, form submission, input validation)
<html>
<head>
<title>Intro to Javascript</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<style>
body { background: #358; color: #fff; padding: 50px; font-family: sans-serif; }
.row {
background: rgba(255, 255, 255, 0.5);
@mayfer
mayfer / actions.rb
Created March 11, 2015 18:27
Simple Ajax Sinatra example
require 'sinatra/json'
def get_click_data
{
count: Click.count,
last_timestamp: Click.all.order('created_at DESC').first.created_at
}
end
get '/' do
@mayfer
mayfer / player.rb
Created March 6, 2015 22:51
Introduction to Objects and Classes
class Person
attr_accessor(:name, :email)
# class variable
@@emails = []
@@population = 0
def initialize(name, email)
@mayfer
mayfer / clock.rb
Created February 28, 2015 00:28
Algorithms lecture notes
# find angle between clock hands
def angle(hour, minutes)
hour += (minutes / 60.0)
angle_hour = hour * 30
angle_mins = minutes * 6
(angle_hour - angle_mins).abs
@mayfer
mayfer / new.html.erb
Created February 23, 2015 18:56
Intro to Sinatra
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="/style.css" />
</head>
<body>
<% if @loggedin == true %>
welcome, <%=@username %>
<a href='/logout'>Log out</a>
@mayfer
mayfer / index.html
Created February 21, 2015 01:07
Node Websockets example (chat + cursors)
<!doctype>
<html>
<head>
<title>hi</title>
<style>
body, html {
background: #aaa;
font-family: monospace;