Skip to content

Instantly share code, notes, and snippets.

@bcardiff
bcardiff / list-deps.cr
Last active April 29, 2024 10:09
List binary dependencies to build a minimal docker image from scratch
unless ARGV.size > 0
puts " Missing executable file argument"
puts " Usage (in a Dockerfile)"
puts " RUN crystal run ./path/to/list-deps.cr -- ./bin/executable"
exit 1
end
executable = File.expand_path(ARGV[0])
unless File.exists?(executable)
$ ./bin/crystal --version
Using compiled compiler at .build/crystal
Crystal 1.8.0-dev [58fb584cb87] (2023-01-21)

LLVM: 14.0.1
Default target: aarch64-apple-darwin21.6.0
@bcardiff
bcardiff / README.md
Last active February 19, 2022 15:13
hex with alpha in p5js
@bcardiff
bcardiff / ast_dump.cr
Created June 30, 2015 21:58
Crystal AST dump
require "compiler/crystal/**"
class Object
def dump_inspect(io, level)
io << " " * level
to_s(io)
io << " :: " << self.class
end
end
@bcardiff
bcardiff / channel-select.md
Created October 8, 2019 20:11
Blocking and non-blocking channel actions

Blocking and non-blocking channel actions

Channel#receive vs Channel#receive? differs on their behavior for closed channels. Using them directly is always blocking.

Performing an operation over a closed and closing the channel during the operation must behave in the same way.

Channel#receive

@bcardiff
bcardiff / README.md
Created April 17, 2020 20:08
VSCode CodeLLDB Crystal Configuration

These configuration will let you use CodeLLDB extension to debug Crystal programs.

Store them as .vscode/tasks.json and .vscode/launch.json in your project.

When starting the debugging the current file will be built in the bin/ folder (as in shards build).

jMvTWMvPdD

Note: You will need to set the crystal binary path in tasks.json "command" field and the whole path in launch.json "initCommands". Read more at #8538

@bcardiff
bcardiff / scoped_cache.rb
Created March 7, 2013 16:07
Rails Scoped Cache allows seamless usage of cache island that can be deleted at once. Also default expiration could be achieved by cache options. Further reference: http://quickleft.com/blog/faking-regex-based-cache-keys-in-rails
class ScopedCache
attr_reader :name
def initialize(cache, name, options = nil)
@cache = cache
@name = name
@options = options
end
def read(key)
data = [{"Brian", 42}, {"Alan", 30}, {"Brian", 32}]
# unstable
data.sort_by &.[0] # => [{"Brian", 42}, {"Alan", 30}, {"Brian", 32}]
# stable
data.map_with_index { |d,i| {d,i} }.sort_by { |di| {di[0][0], di[1]} }.map { |di| di[0] } # => [{"Alan", 30}, {"Brian", 42}, {"Brian", 32}]
@bcardiff
bcardiff / Dockerfile
Created January 22, 2020 13:45
Generate rails-assets packages
FROM ruby:2.2
RUN \
# node
curl -sL https://deb.nodesource.com/setup_5.x | bash - && \
apt-get install -y nodejs && \
# cleanup
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
@bcardiff
bcardiff / local_variable_hle_check.cr
Created November 7, 2019 18:55
Sample to use FileCheck with Crystal
# The lock is at the end of the unions
#
# CHECK: %"(Bool | Int32)" = type { i32, [1 x i64], i32 }
local_var = uninitialized Int32 | Bool
# After the alloca the lock is initialized as free (1)
#
# CHECK: %local_var = alloca %"(Bool | Int32)"
# CHECK: %0 = getelementptr inbounds %"(Bool | Int32)", %"(Bool | Int32)"* %local_var, i32 0, i32 2