Skip to content

Instantly share code, notes, and snippets.

@rogerleite
rogerleite / PlainEnglish.livemd
Created Apr 30, 2021
Interpret and evaluate arithmetic expressions written in plain English (Livebook Elixir)
View PlainEnglish.livemd
# Arithmetic expressions in plain English
## Interpret and evaluate arithmetic expressions written in plain English
Example: "one plus two times four"
* numbers are [zero-ten]
* numbers can be negative, for example: "negative five"
* "plus" and "times" are the only supported operations natural order of operations apply, multiply before add.
* "negative" is optional string before the number and is not an operation "one minus two" is expressed as "one plus negative two"
@rogerleite
rogerleite / defensive-ruby.rb
Created Jun 12, 2019
Example of defensive ruby
View defensive-ruby.rb
# example 1: adopting default values and trying to not break things
def adapter(action)
action = action.to_sym unless action.is_a?(Symbol)
batch = {
active_products: "ProductsActive",
inactive_products: "ProductsInactive",
download_products: "ProductsDownload"
}
batch.fetch(action, "UnknownAction")
end
@rogerleite
rogerleite / gemfile_inline.rb
Created Jul 3, 2015
Template for ruby script with Gemfile inline and thor to map actions and arguments
View gemfile_inline.rb
#!/usr/bin/env ruby
begin
require "bundler/inline"
rescue LoadError => e
puts "You should install bundler with >= 1.10.3 version."
puts "* Current Ruby: #{`ruby -v`}"
puts "* Current Bundler: #{`gem list bundler`}"
puts "* Original Exception: \"#{e.message}\""
exit 1
@rogerleite
rogerleite / list-units
Created Apr 15, 2015
fleetctl list-units
View list-units
UNIT MACHINE ACTIVE SUB
api_v28.web.1.service 15499c5f.../10.21.2.149 active running
dashboard_v34.web.1.service c98d2f7c.../10.21.1.230 active running
deis-builder.service 15499c5f.../10.21.2.149 activating start-post
deis-cache.service c98d2f7c.../10.21.1.230 active running
deis-controller.service 15499c5f.../10.21.2.149 active running
deis-database.service 25992000.../10.21.1.229 active running
deis-logger.service 25992000.../10.21.1.229 active running
deis-logspout.service 15499c5f.../10.21.2.149 active running
deis-logspout.service 25992000.../10.21.1.229 active running
View fluentd_links.md
@rogerleite
rogerleite / converters.rb
Last active Sep 17, 2018
Ruby and CSV examples
View converters.rb
require "csv"
require "date"
puts CSV::HeaderConverters.keys.inspect # => [:downcase, :symbol]
# Add new header converter
CSV::HeaderConverters[:remap] = lambda do |raw_value|
raw_value = raw_value.to_sym
case raw_value
when :country
@rogerleite
rogerleite / _install.md
Last active Mar 9, 2020
Some scripts to install things
View _install.md

Introduction

Some install scripts. Target to work with Ubuntu 12 or greater.

Table of Scripts

@rogerleite
rogerleite / test.sh
Created Jun 26, 2013
Using Shell Script to test your server
View test.sh
#!/bin/bash
URL=http://localhost:8080
## Unit-Testable Shell Scripts (http://eradman.com/posts/ut-shell-scripts.html)
typeset -i tests_run=0
function try { this="$1"; }
trap 'printf "$0: exit code $? on line $LINENO\nFAIL: $this\n"; exit 1' ERR
function assert {
let tests_run+=1
@rogerleite
rogerleite / Gemfile
Last active Dec 16, 2015
CSV data generator for lazy people like me.
View Gemfile
source "https://rubygems.org"
gem "faker", "~> 1.1.2"
gem "uuid", "~> 2.3.7"