Skip to content

Instantly share code, notes, and snippets.

@DougEverly
DougEverly / make_layout.cr
Created June 27, 2019 19:35
Creates a directory structure of nested components loaded from a TSV file.
require "file_utils"
ROOT_PATH = Path.home / Path.new("Documents/Layout")
class Layout
getter parentId : Int32?
getter container : Bool
property children : Array(Layout)
def initialize(@id : Int32, @parentId : Int32?, @name : String, @type : String, @container : Bool)
@DougEverly
DougEverly / kwargs and splats.rb
Created August 3, 2017 19:56
kwargs and splats
require 'pp'
def f(a, b, *args, foo: "foo", required:, times: 2, **kwargs)
puts a, b
pp args
puts foo
puts times
pp kwargs
puts required
end
@DougEverly
DougEverly / prune_sidebar.cr
Created August 1, 2017 20:20
Removes types-list sidebar from crystal api docs
require "xml"
puts "Hello"
if file = ARGV.shift?
xml = File.read(file)
root = XML.parse(xml)
if found_nodes = root.xpath("//div[@id='types-list']").as(XML::NodeSet)
found_nodes.each do |found_node|
puts found_node.attributes
enum Control
Done
end
class Worker(T)
def initialize
@channel = Channel(T|Control).new(10)
@done = Channel(Control).new
struct Int32
def to(t : Int32.class)
self
end
def to(t : Int64.class)
self.to_i64
end
def to(t : String.class)
struct Int32
def to(t : Int32.class)
self
end
def to(t : Int64.class)
self.to_i64
end
def to(t : String.class)
struct Int32
def to(t : Int64.class)
self.to_i64
end
end
a = 13_i32
b = a.to(Int64)
def inner_scope(*args)
return yield(*args)
end
def run : Int32
# must declare i here to use in this scope
i = inner_scope(3, 2) { |args|
x, y = args
i = x + y
}
def foo
yield
end
def run : Int32
# must declare i here to use in this scope
i = nil
foo {
i = 2
}
ch = Channel(Nil).new
alias TH = Thread(Nil, Nil)
threads = Array(TH).new
threads << TH.new do
6.times do
sleep 1
puts 1
end
ch.send(nil)
nil