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
class Boolean | |
TRUE_VALUES = [true, 1, :true, "true", "1", "t", "T", "yes", "y", "Y"] | |
FALSE_VALUES = [false, 0, :false, "false", "0", "f", "F", "no", "n", "N"] | |
ALL_VALUES = TRUE_VALUES + FALSE_VALUES | |
InvalidInput = Class.new(StandardError) | |
def initialize(input) | |
@input = input | |
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
### WHY AND HOW | |
# | |
# ConCache has some nice features, the ones we will use here are: | |
# | |
# `get_or_store`: | |
# Retrieve a cached value or store a new one. | |
# Blocks other processes until the value is written, | |
# then the other processes will read the cached value. | |
# | |
# `callback` function: |
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
awk 'NR >= 100 && NR <= 300' file.in | |
sed -n '100,300p' file.in | |
# there are also solutions with head+tail, but it's not very intuitive for me | |
# example with bzipped files: | |
bzcat file.in | awk 'NR >= 100 && NR <= 300' | |
bzcat file.in | sed -n '100,300p' |
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
# Wrap it in a task (or just spawn a process) to not block your IEx shell | |
# or the following function calls: | |
Task.start(fn -> | |
Stream.interval(1000) # in ms, so quite high-resolution loops are possible | |
|> Stream.map(fn (_) -> "Tick!" end) | |
|> Stream.each(fn (v) -> IO.puts(v) end) | |
|> Stream.run # this will block the process forever! | |
end) | |
# For serious usage you also would need to supervise this process |
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
#!/bin/sh | |
CURRENT_DIR=$( cd "$(dirname "$0")"; pwd ) | |
CURRENT_DIR=$( cd ${0%/*} && pwd -P ) | |
echo $CURRENT_DIR |
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
class FunWithStringNext < ActiveRecord::Base | |
def create_identifier | |
total = FunWithStringNext.count | |
# ENUMERATOR: identifier_generator.take(total).last | |
generate_identifier_by_count(total) | |
end | |
def identifier_generator | |
Enumerator.new do |yielder| | |
init = INITIAL_IDENTIFIER.dup |
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
CondTest.run | |
*** &CondTest.cond_with_true/0 *** | |
72.2 sec 536M iterations 0.14 μs/op | |
*** &CondTest.cond_with_atom/0 *** | |
72.8 sec 536M iterations 0.14 μs/op | |
*** &CondTest.cond_with_truthy_evaluation/0 *** | |
76.4 sec 536M iterations 0.15 μs/op |
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
class BaseService | |
class << self | |
def add_error(name, message) | |
error_klass = Class.new(StandardError) do | |
define_method :initialize do |msg = message| | |
super(msg) | |
end | |
end | |
const_set(name, error_klass) |
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
# Stolen from <http://stackoverflow.com/a/9025552/653173> | |
module HashExt | |
def subhash(*keys) | |
keys = keys.select { |k| key?(k) } | |
Hash[keys.zip(values_at(*keys))] | |
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
defmodule SigilZ do | |
defmacro sigil_z(input, flags) do | |
quote do | |
[input: unquote(input), flags: unquote(flags)] | |
end | |
end | |
end | |
# then: |