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
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 |
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
# HTTP | |
require "net/http" | |
Net::HTTP.start(host) do |http| | |
response = http.send_request("FOO", "/", nil, nil) | |
puts response.body | |
end | |
# HTTPS |
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
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} |
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
# 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 |
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
# 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 () | |
{ |
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 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 |
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: rubocop-rspec | |
AllCops: | |
Include: | |
- "**/*.gemspec" | |
- "**/*.podspec" | |
- "**/*.jbuilder" | |
- "**/*.rake" | |
- "**/*.opal" | |
- "**/Gemfile" |
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
# 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", |
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
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) |
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
# 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: |