Skip to content

Instantly share code, notes, and snippets.

View MaherSaif's full-sized avatar

Maher Saif MaherSaif

View GitHub Profile
require 'logger'
require 'rubygems'
require 'wirble'
require 'pp'
Wirble.init
Wirble.colorize
# require 'utility_belt'
if ENV.include?('RAILS_ENV')&& !Object.const_defined?('RAILS_DEFAULT_LOGGER')
Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
@MaherSaif
MaherSaif / kata.rb
Created October 15, 2010 19:56 — forked from notahat/kata.rb
def kata(digits = [], has_pair = false)
if digits.inject {|sum, digit| sum + digit } == 15
has_pair ? [digits] : []
else
start = (digits.last || 0) + 1
(start..9).inject([]) do |result, digit|
result + kata(digits + [digit], has_pair) + kata(digits + [digit, digit], true)
end
end
end
@MaherSaif
MaherSaif / kata.rb
Created October 15, 2010 19:56 — forked from notahat/kata.rb
def kata(digits = [], sum = 0, has_pair = false)
if sum < 15
start = (digits.last || 0) + 1
(start..9).inject([]) do |result, digit|
result +
kata(digits + [digit], sum + digit, has_pair) +
kata(digits + [digit, digit], sum + digit + digit, true)
end
elsif sum == 15 && has_pair
[digits]
server {
access_log /opt/nginx/logs/test_server.access.log main buffer=32k;
error_log /opt/nginx/logs/test_server.error.log info;
expires 6h;
listen 2300 default rcvbuf=64k backlog=128;
root /opt/apps/test_server/current/public;
server_name test_server.com www.test_server.com;
passenger_enabled on;
}
pid /opt/nginx/logs/nginx.pid;
# Run as the nginx user
user nginx nginx;
worker_processes 2;
error_log /opt/nginx/logs/error.log notice;
events {
worker_connections 1024;
use epoll;
@MaherSaif
MaherSaif / webapp.rb
Created November 23, 2010 22:49 — forked from igrigorik/webapp.rb
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
module RuleBook
def RuleBook.add_rules(object, instance_or_class, rules)
instance_or_class = instance_or_class.to_s.downcase.strip.to_sym
raise(ArgumentError, "'instance_or_class' must equal :instance or :class") unless [:instance, :class].include?(instance_or_class)
raise(ArgumentError, "'rules' must be a Hash") unless rules.is_a?(Hash)
unless object.instance_variable_get(:@_rulebook_initiated) # Class instance variable. Not class variable. Not instance variable. Is confusing.
object.instance_variable_set(:@_rulebook_initiated, true)
object.instance_variable_set(:@_rulebook, {:instance=>{}, :class=>{}}) # @_rulebook is actually two rulebooks, instance and class
end
# Rake task to launch multiple Resque workers in development/production with simple management included
require 'resque/tasks' # Require Resque tasks
namespace :workers do
# = $ rake workers:start
#
# Launch multiple Resque workers with the Rails environment loaded,
# so they have access to your models, etc.
# Usage:
respond_to :rss, :only => [:index]
def index
@deals = Deal.by_city(@city).limit(15)
respond_with(@deals) do |format|
format.rss { render :rss => @deals, :title => "KupiKon", :item => { :title => :title, :description => :description }}
end
end
ActiveRecord::Base.connection.increment_open_transactions
ActiveRecord::Base.connection.begin_db_transaction
at_exit do
ActiveRecord::Base.connection.rollback_db_transaction
ActiveRecord::Base.connection.decrement_open_transactions
end