Skip to content

Instantly share code, notes, and snippets.

View asaaki's full-sized avatar
🦀
I may be slow to respond. Ping me on Twitter instead: https://twitter.com/asaaki

Christoph Grabo asaaki

🦀
I may be slow to respond. Ping me on Twitter instead: https://twitter.com/asaaki
View GitHub Profile
@asaaki
asaaki / private_constants.rb
Created November 1, 2017 12:50
[Ruby] Private constants check
require 'active_support'
module MyConcern
extend ActiveSupport::Concern
class PrivateClass
def self.pcall
:pcall_result
end
end
@asaaki
asaaki / do.rb
Last active July 28, 2017 14:45
What Ruby does …
# Ruby's do notation
# Don't do!
module Foo; end
module FooDo do; end
#=> SyntaxError: unexpected keyword_do, expecting ';' or '\n'
class Foo; end
class FooDo do; end
@asaaki
asaaki / hash-with-indifferent-access.rb
Created July 3, 2017 11:38
Simple hash with indifferent access (string and symbol keys)
# ref:
# https://github.com/sinatra/sinatra/blob/8c2504c/lib/sinatra/base.rb#L1082-L1085
module HashWithIndifferentAccess
def self.new
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end
end
# ref:
# https://github.com/sinatra/sinatra/blob/8c2504c/lib/sinatra/base.rb#L1068-L1080
@asaaki
asaaki / value.sh
Last active May 9, 2017 11:38
Shell scripting: check if value is in string as conditional
#!/bin/sh
POSSIBLE_VALUES="foo bar baz"
TEST_VALUE=$1
echo $POSSIBLE_VALUES | grep $TEST_VALUE 1>/dev/null
exit $?
@asaaki
asaaki / operator_foo.rb
Last active February 26, 2017 14:53
[Ruby] `&&` vs. `and`
# Helper:
# alternative `puts` with a non-nil return value
# similar to many method calls which could be useful with logical operations
def puts_with_true(arg)
puts(arg)
true
end
# puts returns `nil` by default
puts 1 && 2
@asaaki
asaaki / elixir-library-error-handling.ex
Created February 16, 2017 13:15
Error Handling in Elixir Libraries
# example from:
# http://michal.muskala.eu/2017/02/10/error-handling-in-elixir-libraries.html
defmodule YourLibrary do
defmodule Error do
defexception [:reason]
def exception(reason),
do: %__MODULE__{reason: reason}
def message(%__MODULE__{reason: reason}),
@asaaki
asaaki / .iex.exs
Last active February 1, 2017 16:40
Automagically alias namespaced modules on IEx shell start
Code.compiler_options(ignore_module_conflict: true)
base_dir = "web/models"
if File.exists?(base_dir) do
for model_file <- File.ls!(base_dir) do
for {mod, _bytes} <- Code.load_file(model_file, base_dir) do
Code.eval_quoted(quote(do: alias unquote(mod)), [mod: mod])
end
end
end
Code.compiler_options(ignore_module_conflict: false)
@asaaki
asaaki / bench.rb
Created January 18, 2017 13:20
[Ruby] Benchmark Performance Array#map vs. Set#map
# frozen_string_literal: true
# Gemfile:
# # frozen_string_literal: true
# source 'https://rubygems.org'
# gem 'benchmark-ips'
require 'bundler/setup'
require 'benchmark/ips'
require 'set'
@asaaki
asaaki / keybase.md
Created May 8, 2016 17:16
keybase.md

Keybase proof

I hereby claim:

  • I am asaaki on github.
  • I am asaaki (https://keybase.io/asaaki) on keybase.
  • I have a public key ASBo4idVp9JxHEySo-IQ8g3-CFIvbUj93GGL_H8j8YpURwo

To claim this, I am signing this object:

@asaaki
asaaki / tap_with_conditional_return.rb
Created February 10, 2016 14:20
Poor Man's TapWithConditionalReturn
[some_data.fetch('a_string', fallback_string)].reduce(replacer) { |r, s| s == criteria ? r : s }
# or a way simpler workaround:
(origin = some_data.fetch('a_string', fallback_string)) == criteria ? replacer : origin