Skip to content

Instantly share code, notes, and snippets.

View naimrajib07's full-sized avatar

Naim Rajiv naimrajib07

View GitHub Profile
@naimrajib07
naimrajib07 / hash.rb
Created November 3, 2014 15:41
Monkey Patch the Core Ruby Hash class to rename the hash key!
class Hash
def rename_keys(mapping)
result = {}
self.map do |k,v|
mapped_key = mapping[k] ? mapping[k] : k
result[mapped_key] = v.kind_of?(Hash) ? v.rename_keys(mapping) : v
result[mapped_key] = v.collect{ |obj| obj.rename_keys(mapping) if obj.kind_of?(Hash)} if v.kind_of?(Array)
end
result
end
@naimrajib07
naimrajib07 / nginx.conf
Created June 10, 2014 07:08
nginx config for amazon ec2 micro instances.
upstream unicorn {
server unix:/tmp/.sock fail_timeout=0;
#server 127.0.0.1:8080 fail_timeout=0;
}
server {
listen 80 default deferred;
server_name 54.187.156.63;
root /var/www/apps/points_of_light/current/public; #your project public path
try_files $uri/index.html $uri @unicorn;
@naimrajib07
naimrajib07 / unicorn_init.sh
Created June 10, 2014 07:02
unicorn_init shell script for amazon ec2 micro instance.
#!/bin/sh
set -e
# Example init script, this can be used with nginx, too,
# since nginx and unicorn accept the same signals
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT='/var/www/apps/points_of_light/current'
APP_ENV='staging'
PID="/var/www/apps/points_of_light/shared/pids/unicorn.rb.pid"
@naimrajib07
naimrajib07 / unicorn.rb
Created June 10, 2014 06:57
uncorn config for amazon ec2 micro instance.
working_directory "/var/www/apps/points_of_light/current"
pid "/var/www/apps/points_of_light/shared/pids/unicorn.rb.pid"
stderr_path "/var/www/apps/points_of_light/shared/log/unicorn.rb.log"
stdout_path "/var/www/apps/points_of_light/shared/log/unicorn.rb.log"
listen "/tmp/.sock", :backlog => 64;
worker_processes 4
timeout 30
@naimrajib07
naimrajib07 / unicorn_init.sh
Created June 8, 2014 18:55
Make a unicorn initialize shell script.
#!/bin/sh
set -e
# Example init script, this can be used with nginx, too,
# since nginx and unicorn accept the same signals
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/var/www/my_app
APP_ENV=development
PID=$APP_ROOT/tmp/pids/unicorn.pid
@naimrajib07
naimrajib07 / unicorn.rb
Created June 8, 2014 18:35
Unicorn config to setup unicorn as a application server.
working_directory "/var/www/my_app"
pid "/var/www/my_app/tmp/pids/unicorn.rb.pid"
stderr_path "/var/www/my_app/log/unicorn.rb.log"
stdout_path "/var/www/my_app/log/unicorn.rb.log"
listen "/tmp/unicorn.my_app.sock"
worker_processes 4
timeout 30
@naimrajib07
naimrajib07 / nginx.conf
Created June 8, 2014 18:19
Make nginx.conf for the rails app
upstream unicorn {
server unix:/tmp/unicorn.my_app.sock fail_timeout=0;
}
server {
listen 80 default deferred;
# server_name example.com;
root /var/www/my_app/public; #your project public path
try_files $uri/index.html $uri @unicorn;
location @unicorn {
@naimrajib07
naimrajib07 / lambda_test.py
Created May 28, 2014 11:32
Test lambda behavior in python!
__author__ = 'naim'
squares = []
squares.append(lambda n: n*n) # list have only one element and it's index is 0 and content is lambda!
print squares[0](2)
@naimrajib07
naimrajib07 / dictionary.py
Created May 28, 2014 11:24
Working with hash/Dictionary in Python
__author__ = 'naim'
class MyClass:
def __init__(self):
self.x = 1
self.my_hash = {'fname': ('n', 'a', 'i', 'm'), 'lname': 'rajib'}
def getVal(self):
return self.x
@naimrajib07
naimrajib07 / lexical_scoping.py
Created May 28, 2014 11:19
A common pattern that occurs while attempting to use closures, and leads to confusion, is attempting to encapsulate an internal variable using an immutable type. When it is re-assigned in the inner scope, it is interpreted as a new variable and fails because it hasn?t been defined.
__author__ = 'naim'
def iteration():
count = [0] # caution of scoping variable when it is immutable like count = 0, it will local scope for
# iteration() so that used mutable data type like list
def increment_value():
count[0] += 1
return count[0]
return increment_value