Skip to content

Instantly share code, notes, and snippets.

View joeyates's full-sized avatar

Joe Yates joeyates

View GitHub Profile
@joeyates
joeyates / subset-sum.rb
Created April 29, 2017 18:58
Solve the Subset Sum Problem in Ruby using Dynamic Programming
def dump_table(sums)
sums.each.with_index { |row, sum| puts "#{sum}: #{row.inspect}" }
end
def solvable?(set, size, sum)
sums = (0 .. sum).map do |i|
(0 .. size).map do |j|
nil
end
end
@joeyates
joeyates / non_standard_http_verbs.rb
Created January 29, 2017 19:23
Make HTTP Calls with Non-standard Verbs (in Ruby)
# HTTP
require "net/http"
Net::HTTP.start(host) do |http|
response = http.send_request("FOO", "/", nil, nil)
puts response.body
end
# HTTPS
@joeyates
joeyates / gen_server_that_uses_stdio.ex
Created December 26, 2016 20:26
(In tests) how to capture another process's IO
defmodule GenServerThatUsesStdio do
use GenServer
def start_link() do
GenServer.start_link(__MODULE__, [])
end
def handle_cast(message, state) do
IO.puts message
{:noreply, state}
@joeyates
joeyates / elixir-varargs-macro.ex
Created August 6, 2016 23:59
Via a macro, define an Elixir Function with Arity of Provided Arguments
# tags: #code-golf
# Define a method like this:
# `VarArgs.def(:hi, [:there, :world])
# You get a function:
# `def hi(there, world) ...`
defmodule VarArgs do
defmacro def(name, args) do
params = Enum.map(args, fn(a) -> Macro.var(a, nil) end)
quote do
def unquote(:"#{name}")(unquote_splicing(params)) do
# Set up a bash function to cd to a Rails app's current directory and set the prompt
rails_app ()
{
cd "$APP_PATH/current";
export PS1="(\$APP_NAME:\$RAILS_ENV)\w>"
}
my_rails_app ()
{
@joeyates
joeyates / rbenv_local_ruby.sh
Created July 3, 2016 11:13
Build Ruby from Local Sources and add to RBenv
#!/usr/bin/env sh
# Usage:
# $ sh rbenv_local_ruby.sh {VERSION}
# If VERSION is missing, builds and installs 'local-ruby'
VERSION=${1:-local-ruby};
echo $VERSION
./configure --prefix=~/.rbenv/versions/$VERSION
make
@joeyates
joeyates / .rubocop.yml
Last active June 1, 2017 10:19
Cantiere creativo Rubocop Configuration
require: rubocop-rspec
AllCops:
Include:
- "**/*.gemspec"
- "**/*.podspec"
- "**/*.jbuilder"
- "**/*.rake"
- "**/*.opal"
- "**/Gemfile"
@joeyates
joeyates / books2array4csv.jq
Last active December 6, 2018 20:33
jq golf
# Transform Bookrepublic catalogue API books data
# into an input suitable for CSV generation.
# Example call: see transform_array_via_ruby.sh
# we're interested in these fields:
[
"isbn", "title", "description",
"authors", "publisher", "publication_year", "created_at",
"language", "translator",
@joeyates
joeyates / form_encode.ex
Created October 3, 2015 08:59
Do object-scoped from encoding of HTTP parameters
defmodule FormEncode do
def form_encode(params) when is_map(params) do
form_encode([], params)
end
def form_encode(nesting, value) when is_map(value) do
Enum.map_join(value, "&", fn({k, v}) ->
n1 = Enum.reverse(Enum.reverse(nesting), [k])
form_encode(n1, v)
end)
@joeyates
joeyates / run_ruby_tests.sh
Last active January 23, 2017 09:44
Running MRI Ruby Tests
# While modifying Ruby source, it's handy to only run the tests on the file you're modifying.
# See:
# * doc/contributing.rdoc in Ruby source,
# * https://bugs.ruby-lang.org/projects/ruby/wiki/DeveloperHowto
# Run core tests:
make test
# Run all tests: