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
% 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) -> |
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
% 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"), |
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
% 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 |
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
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 |
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
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') |
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
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'): |
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
# 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 |
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
#!/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 = [] |
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 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 |
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 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 |
OlderNewer