Skip to content

Instantly share code, notes, and snippets.

View sebglazebrook's full-sized avatar

Seb Glazebrook sebglazebrook

View GitHub Profile
@sebglazebrook
sebglazebrook / pricing_service.rb
Last active February 4, 2018 00:40
Controlling the API of modules in Ruby
require "payment_service/authorize_payment" # This defines PaymentService::AuthorizePayment class
require "payment_service/authorization_error" # This defines PaymentService::AuthorizatonError class
require "payment_service/successful_authorization_workflow" # This defines PaymentService::SuccessfulAuthorizationWorkflow class
require "payment_service/payment_repository" # This defines PaymentService::PaymentRepository class
module PaymentService
def self.authorize_payment(token)
response = AuthorizePayment.new(token).execute
if response.success?
# This is similar to a controller in rails
# each method represents a verb/action from rest
class User < Resource
include JsonRendering
def options
end
def head
#!/bin/bash
# install homebrew
echo "installing homebrew"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
echo "install homebrew bundle"
brew tap Homebrew/bundle
# pull down a brewfile
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo " deb http://apt.dockerproject.org/repo debian-jessie main" | sudo tee /etc/apt/sources.list.d/docker.list
echo " deb http://cdn.rawgit.com/sebglazebrook/debian-pkg-repo/0bc3575813da95c899b72a8a91b06e9c8e6d35e4 jessie main" | sudo tee /etc/apt/sources.list.d/sebglazebrook.list
sudo apt-get update
sudo apt-get install --yes --force-yes seb-dev-env
let file_path = "my.test".to_string();
let mut file = File::create(file_path).unwrap();
file.write_all("ls".as_bytes());
let mut process = Command::new("source")
.arg(file_path)
.spawn()
.unwrap_or_else(|e| { panic!("failed to execute child: {}", e) });
struct Foo<'a> {
collection: Vec<usize>,
matches: Vec<&'a usize>,
}
impl<'a> Foo<'a> {
pub fn new() -> Self {
Foo { collection: vec![1,2,3,4,5,6] , matches: vec![] }
}
use std::sync::{Arc, Mutex};
use std::sync::mpsc::*;
fn main() {
let (tx, rx):(Sender<_>, Receiver<_>) = channel();
}
src/main.rs:21:18: 21:42 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]
src/main.rs:21 let (tx, rx):(Sender<_>, Receiver<_>) = channel(); //???????
^~~~~~~~~~~~~~~~~~~~~~~~
fn main() {
let monitors = monitor_factory::create_all();
for monitor in monitors.iter() {
let current_monitor = monitor.clone();
thread::spawn(move || {
current_monitor.run();
}).join();
}
}
@sebglazebrook
sebglazebrook / method_logger
Created March 12, 2015 22:48
Method Logger Ruby
require 'active_support'
# To use this make sure you add `include MethodLogger` at the END of your class definition
# Otherwise the methods you want aliased are not declared yet.
module MethodLogger
def self.logger
@@logger ||= Logger.new("#{Rails.root}/log/method_logger.log")
end
@sebglazebrook
sebglazebrook / gist:318e037781e1c2ab1d21
Last active August 29, 2015 14:10
Differing between array like and hash like objects
# Came across an issue when working on a serializer/deserializer
# needed to know whether something was array like or hash like.
class Object
def array_like?
self.respond_to?(:each) && respond_to?(:at)
end
def hash_like?