Skip to content

Instantly share code, notes, and snippets.

@maiha
maiha / 0.md
Last active March 9, 2024 17:58
practical migrating guide to crystal-0.20.4
module Core
end
class Base(T)
include Core
end
class Foo < Base(String)
end
def parse(str)
JSON.parse(str)
end
pp parse("1") # => 1
pp parse("x") # raise JSON::ParseException
begin
v = parse("1")
# use v
module Core(T)
@a : Bool
end
class Base(T)
include Core(Int32)
def initialize
@a = true
end
@maiha
maiha / test.cr
Created November 27, 2016 03:59 — forked from fridgerator/test.cr
class Changeset(T)
def initialize(@instance : T)
end
end
module Cset(T)
def changeset(instance)
Changeset(T).new(instance)
end
end
@maiha
maiha / 0.general.sh
Last active November 23, 2016 20:20
practical migrating guide to crystal-0.20.0 https://crystal-lang.org/2016/11/22/crystal-0.20.0-released.html
#!/usr/bin/env zsh
set -e
# for shards-0.7
rm -rf shard.lock libs
shards update
echo "/lib/" >> .gitignore
# MemoryIO
sed -i "s/MemoryIO/IO::Memory/g" {src,spec}/**/*.cr
% git clone [email protected]:crystal-lang/crystal_lib.git
% cd crystal_lib
% cat > lib_ntohl.cr
@[Include("arpa/inet.h")]
lib LibC
fun ntohl
end
% crystal src/main.cr -- lib_ntohl.cr
lib LibC
alias Entry = Hash(Symbol, File::Stat | String)
def awalkDir(dir, block : Entry -> Nil)
Dir.entries(dir).each do |fname|
next if fname == "." || fname == ".."
current = File.join(dir, fname)
begin
127.0.0.1:6379> zadd zset 100 a
(integer) 1
127.0.0.1:6379> zadd zset 200 b
(integer) 1
127.0.0.1:6379> eval "return {redis.call('ZSCORE',KEYS[1],KEYS[2]),redis.call('ZRANK',KEYS[1],KEYS[2])}" 2 zset b
1) "200"
2) (integer) 1
127.0.0.1:6379> script load "return {redis.call('ZSCORE',KEYS[1],KEYS[2]),redis.call('ZRANK',KEYS[1],KEYS[2])}"
"b6b9e64ed4cc349469e0f24c49469c6d6fcdfc5a"