Skip to content

Instantly share code, notes, and snippets.

View devmug's full-sized avatar

Mike devmug

View GitHub Profile
#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Update 7 Oct 2010:
# - This example does *not* appear to work with Chrome >=6.0. Apparently,
# the WebSocket protocol implementation in the cramp gem does not work
# well with Chrome's (newer) WebSocket implementation.
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby
@devmug
devmug / gist:6988466
Last active December 25, 2015 14:08
postgres recursive query.
First the DDL:
CREATE TABLE node (
id SERIAL,
label TEXT NOT NULL, -- name of the node
parent_id INT,
PRIMARY KEY(id)
);
NOTICE: CREATE TABLE will create implicit sequence "node_id_seq" for serial column "node.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "node_pkey" for table "node"
@devmug
devmug / sti-rails3
Created September 14, 2013 09:16
Single table inheritance (sti's) rails 3
Single Table Inheritance in Rails 3
What is Single Table Inheritance?
STI allows you to create subclasses of a particular database table.
Using a single table, you can cast rows to specific objects that extend the base model.
How to create STI relationships in Rails
Lets say we have a model Computer
@devmug
devmug / ruby iterate hash
Created September 14, 2013 06:42
sorting and iterating a hash in ruby
flash.each do |key, value|
puts "#{key}"
puts "#{value}"
end
Calling sort on a hash converts it into nested arrays and then sorts them by key, so all you need is this:
puts h.sort.map {|k,v| ["#{k}----"] + v}
puts h.sort
@devmug
devmug / Ruby - Periodic Process
Last active December 22, 2015 09:28
Ruby's sleep is not very accurate and Eventmachine's timer is also not very good. So, If you need to process one periodic job with some interval, this approach might be helpful. There is a compensation for sleep and you just need to define your process.
class Service
def initialize
@interval = 1.0
@start_time = Time.now
end
def start
# Update the start-time
@start_time = Time.now
# run loop
loop do
@devmug
devmug / rails custom validators
Created September 3, 2013 09:44
Shopping cart validators
module CustomValidators
class Emails
def self.email_validator
/\A(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})\z/i
end
end
class Numbers
def self.phone_number_validator