This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |