Skip to content

Instantly share code, notes, and snippets.

View Veejay's full-sized avatar

Bertrand Chardon Veejay

View GitHub Profile
@Veejay
Veejay / matching.rb
Created April 19, 2012 18:38
Dumb and basic version of pattern matching construct in Ruby
class Array
def extract_options!
last.is_a?(::Hash) ? pop : {}
end
end
def match(*args)
rules = args.extract_options!
if rules.has_key?(*args)
rules[*args].call
SELECT
date(created_at) as ordered_date,
sum(price) as total_price
FROM
orders
GROUP BY
date(created_at)
HAVING
sum(price) > 100
#include <stdio.h>
#include <string.h>
char* reverse(char *s)
{
int i, placeholder;
int l = strlen(s) - 1;
for(i = 0; i < (l / 2); i++){
placeholder = *(s+i);
*(s+i) = *(s+l-i);
ClientDashboard::Application.routes.draw do
namespace :admin do
resources :brands
resources :email_messages
resources :users
resources :memberships do
post 'new_by_brand', :on => :collection
end
resources :clients do
respond_to :json
def index
if params[:secret].eql? SOME_API_TOKEN
respond_with Company.all_with_users_and_projects
else
render { head :forbidden } and return
end
end
@Veejay
Veejay / test.js
Created April 11, 2012 15:58
JSON object
{ companies: [
{ name: “company name”
, users: [
{ id: 1
, first_name: “rob”
, last_name: “sterner”
, project_ids: [1,2,3,4]
}
]
, projects: [
int
mbtowc(wchar_t *p, char *s, size_t n)
{
long l;
int c0, c, nc;
Tab *t;
if(s == 0)
return 0;
@Veejay
Veejay / password_reset_spec.rb
Created March 26, 2012 21:58
Specs for password reset
require 'spec_helper'
describe RegistrationsController do
describe "Forgetful user gets to the user page. He should have a forgot password link" do
it "should display a template containing the string t('sessions.new.forgot_password')" do
end
end
describe "User provides an email address" do
it "should display a page containg an appropriate text field and a submit button" do
@Veejay
Veejay / json_serializer.rb
Created March 23, 2012 01:17
JSON Serializer built on top of Jbuilder with emphasis on ease of use
module StaffPlan::JsonSerializer
def serialize(*args)
options = args.extract_options!
if args.first.is_a?(Array) or args.first.is_a?(ActiveRecord::Relation)
serialize_collection(args.first, options)
else
serialize_instance(args.first, options)
end
end
@Veejay
Veejay / gist:2047303
Created March 15, 2012 22:09
Adds an instance method to arrays that returns an array containing all the elements that satisfy a criterion defined by a block. The elements are REMOVED from the original array.
Array.class_eval do
def extract!
if block_given?
[].tap do |rejects|
delete_if do |element|
yield(element) and rejects << element
end
end
else
self