Skip to content

Instantly share code, notes, and snippets.

@mmiliaus
mmiliaus / nginx.conf
Created April 17, 2011 10:53
nginx.conf to run nginx server which proxies to ruby daemon
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
@mmiliaus
mmiliaus / basic.rb
Created April 18, 2011 10:00
Ruby Code Snippets
#Strip HTML tags
def strip_html(allowed = [])
str = self.strip || ''
str.gsub(/<(\/|\s)*[^(#{allowed.join('|') << '|\/'})][^>]*>/,'')
end
@mmiliaus
mmiliaus / Code that writes code.rb
Created April 22, 2011 09:46
Ruby Metaprogramming
* instance_eval, class_eval, module_eval
* instance_eval, evaluates code on instance level:
class Bongas
class << self
def nu_kazka
puts "Nu Kazka Bongas"
end
end
end
Bongas.instance_eval { nu_kazka } # -> "Nu Kazka Bongas"
@mmiliaus
mmiliaus / js_notes.js
Created April 26, 2011 20:43
JavaScript notes
What JavaScript considers as False: false, null, undefined, '', 0, NaN.
---
Objects are passed around by reference. They are never copied:
var x = stooge;
x.nickname = 'Curly';
var nick = stooge.nickname;
// nick is 'Curly' because x and stooge
// are references to the same object
# =stylesheet_include_tag
def stylesheet_include_tag(filename)
mtime = File.new(File.join(APP_PATH,"public","stylesheets","#{filename}.css")).mtime
%Q(<link rel="stylesheet", href="/stylesheets/#{filename}.css?#{mtime.to_i}"></link>)
end
...
@mmiliaus
mmiliaus / gist:972444
Created May 14, 2011 17:59
Git in 20 minutes
=config
# use colors in git output
git config --global color.ui true
=basics
git branch -d test //delete branch
=.gitignore file
*a # ignore all with .a ext
!lib.a # except file "lib.a"
@mmiliaus
mmiliaus / gist:973506
Last active September 25, 2015 19:38
Pro Linux System Administration
=users and groups
# add user into 'admin' group, only users in 'admin' group can use 'sudo'
sudo usermod -G admin username
# useradd defaults
/etc/default/useradd
# check to what groups user belongs to
id username
# ubuntu
adduser, addgroup
def match! battle, winner, loser
r_a = winner.rating
r_b = loser.rating
max = battle.max.to_i
min = battle.min.to_i
th = ( max - min ) / 2
e_a = 1.0 / ( 1 + 10 ** ( (r_b - r_a) / 400 ))
e_b = 1.0 / ( 1 + 10 ** ( (r_a - r_b) / 400 ))
@mmiliaus
mmiliaus / cs.py
Created December 23, 2011 13:03
python cheatsheet
def foo(x, y=10, **kwargs):
# dictionaries
dict = {'a':1, 'b':23, 'c':'eggs'}
del dict['b']
dict.has_key('e')
# list comprehension
[x for x in range(5) if x%2 == 0]
# exception
@mmiliaus
mmiliaus / Getting img pixel data
Created December 24, 2011 21:23
cold JS cuts
canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d");
canvas.className = "myClass";
canvas.id = "myId";
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0);
pixelData = ctx.getImageData(0, 0, image.width, image.height);