Skip to content

Instantly share code, notes, and snippets.

@arunthampi
arunthampi / gist:8317
Created September 1, 2008 16:21
An Erlang module which starts a registered spawned process which periodically executes
% Warming the engine for Erlang
% This Erlang module starts a clock under a registered process name
-module(clock).
-export([start/2, stop/0]).
% What this does is register the current process with the atom clock
% And the process which is registered is a spawned fun (fun is an anonymous function, similar to
% a block in Ruby) which calls the tick function.
start(Time, Fun) ->
% Example of Erlang guards
-module(double).
-export([double/1]).
%% If guard expressions are separated by a comma, it means AND.
%% If guard expressions are separated by a semi-colon, it means OR.
double(X) when X > 0; X < 0 -> X * 2;
% If a function has multiple lines, each line (except the last), must be
% terminated by a comma
double(X) when X =:= 0 -> io:format("Trying to double a zero homie?~n"),
% My attempt to document all the Erlang-isms that I come across
% mostly from the wonderful book by Joe Armstrong (which I highly recommend)
% Extracting values from tuples
Point = {point, 10, 45}.
{point, X, Y} = Point.
X. % Will return 10
Y. % Will return 45.
% Extracting elements from a list
require 'rubygems'
require 'amqp'
require 'mq'
require 'evented_net'
def log *args
p args
end
# If running on Linux, use EventMachine.epoll, on Mac OS X, use EventMachine.kqueue
require 'rubygems'
require 'json'
require 'benchmark'
# all_dbs.js > Output from CouchDBs http://127.0.0.1:5984/_all_dbs
# Simple array of strings
js = File.read('all_dbs.js')
# sg.js > Internal API for hotels in a city (Singapore)
# Slightly more complicated nested data structures including strings and numbers and arrays and hashes
sg = File.read('sg.js')
from twisted.internet import reactor, protocol, defer
from twisted.protocols import basic
class SimpleStompProtocol(protocol.Protocol):
def connectionMade(self):
self.connect("", "")
def dataReceived(self, data):
frame = self.unpackFrame(data)
if(frame['cmd'] == 'CONNECTED'):
# Stolen from http://snippets.dzone.com/posts/show/6621 (for me to remember)
# for this example the folder structure should be as follow
#
# --+ mailer
# |-- image.jpg
# |-- mailer.rb (this file)
# |--+ notifier
# |-- email.rhtml
@arunthampi
arunthampi / split.rb
Created April 20, 2009 08:52
Split File into multiple pieces
#!/usr/bin/env ruby
input_file = ARGV[0]
# This function is stolen from:
# http://snippets.dzone.com/posts/show/3486
def chunk_array(array, pieces=2)
len = array.length;
mid = (len/pieces)
chunks = []
class Pet < ActiveCouch::Base
has :name, :which_is => :text, :with_default_value => 'Tom'
end
class Person < ActiveCouch::Base
has :name, :which_is => :text, :with_default_value => 'McLovin'
has :age, :which_is => :number, :with_default_value => 0
has_many :dogs, :class => Pet
has_many :cats, :class => Pet
has_many :pets # Automagically assumes that 'pets' corresponds to the class Pet
class Person < SuperModel::Base
# If you don't specify a 'type', it defaults to string</span>
# while converting to JSON
has :name
end
# Create a SuperModel object
p = Person.new(:name => 'McLovin')
# Another way you can create a SuperModel object