Skip to content

Instantly share code, notes, and snippets.

View asaaki's full-sized avatar
🦀
Oh crab!

Christoph Grabo asaaki

🦀
Oh crab!
View GitHub Profile
@asaaki
asaaki / callables_spec.rb
Created March 2, 2018 09:44
callables in specs
require 'spec_helper'
module CallableSubject
def callable_subject(&_block)
subject { -> { yield } }
end
end
RSpec.configure do |c|
c.extend(CallableSubject)
@asaaki
asaaki / playground.rs
Created January 26, 2018 08:42 — forked from anonymous/playground.rs
Rust code shared from the playground
use std::ops::Deref;
struct StateFn(fn(&mut Machine) -> StateFn);
impl Deref for StateFn {
type Target = fn(&mut Machine) -> StateFn;
fn deref(&self) -> &Self::Target {
&self.0
}
@asaaki
asaaki / 0___rust_serde_json_wasm_parcel___0.md
Last active January 28, 2023 14:14
Rust-to-JS JSON string exchange (parcel.js, wasm)

Rust, serde-json, wasm, parcel.js

With this combination it is fairly easy to get things done in Rust and utilize it in JavaScript. Yay!

@asaaki
asaaki / hello.rs
Last active January 26, 2018 01:57
parcel.js/Rust: return a string
/*
gist is: return a ref to c_char and the length of it (via 2 functions)
C example: https://wasdk.github.io/WasmFiddle//?w590f
*/
use std::ffi::CString;
use std::os::raw::c_char;
static HELLO: &'static str = "hello from rust";
@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}),