Skip to content

Instantly share code, notes, and snippets.

@mayfer
mayfer / index.erb
Created November 14, 2014 19:57
Basic HTTP get/post example setup with sinatra
<!doctype html>
<html>
<head>
<title>hmmm</title>
<style>
html, body { background: #377; font-family: monospace; color: #fff; }
</style>
</head>
<body>
@mayfer
mayfer / intro_js.md
Last active August 29, 2015 14:10
Introduction to JS

Global vs. local variables

  • Local variables are defined with the var keyword.
  • Variables defined without var are automatically global.

Function declaration and calling

  • Two ways to define functions:
    • function someFunc(arg1, arg2) {}
    • var someFunc = function(arg1, arg2) {}
  • All arguments are inherently optional. Calling someFunc() without arguments will NOT error out. The arguments will have undefined values.
@mayfer
mayfer / 0.js
Created December 8, 2014 20:05
node examples, step by step.
// run this with "node 0.js"
console.log("Hello World");
@mayfer
mayfer / find_number.rb
Created December 11, 2014 19:12
Algorithms and Data structures
# complexity O(log N)
def find_number(numbers, find_number)
middle = numbers.size / 2
first = 0
last = numbers.size - 1
loop do
@mayfer
mayfer / ws.html
Created December 16, 2014 19:02
Node, Websockets (socket.io), Prototypical inheritance
<!doctype>
<html>
<head>
<title>hi</title>
<style>
body, html {
background: #aaa;
font-family: monospace;
@mayfer
mayfer / gist:3f36ebd64f1515b03094
Created January 24, 2015 19:26
HTTP server response demo
require 'sinatra'
post '/' do
"You sent: #{params.inspect}"
end
get '/' do
<<-eos
<!doctype html>
@mayfer
mayfer / urls.txt
Created January 27, 2015 21:41
Exploring URL routes
Hate mail website
=====================
GET / -> all hate mails (10 per page)
# pagination, two options...
GET /?page=:page_num -> get particular page
GET /page/:page_num/ -> get particular page
@mayfer
mayfer / palindrome.rb
Created February 5, 2015 22:19
Find longest palindromes
def find_longest_palindromes
words = File.open('/usr/share/dict/words').read
longest_words = []
words.each_line do |line|
word = line.strip
@mayfer
mayfer / select.rb
Created February 5, 2015 22:20
Block example (re-writing select with puts)
def select(arr)
arr.each do |elem|
if yield elem
puts elem
end
end
end
select([1, 5, 7, 2, 4, 6]) do |elem|
elem % 2 == 1
@mayfer
mayfer / files_controller.rb
Created February 6, 2015 19:29
File upload
class FilesController < ApplicationController
def index
end
def upload
upload = params[:babyfile]
name = upload.original_filename
directory = "public/data"
# create the file path