This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/sh | |
# Count lines of multiple files (recursively): | |
find source_dir -name "*.ext" -exec wc -l {} + | |
# 7 digit sha1 transformed timestamp name: | |
### simple: date +%s%N | \ | |
### more unique | |
/usr/lib/systemd/systemd-timestamp | \ | |
sha1sum | \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support' | |
module MyConcern | |
extend ActiveSupport::Concern | |
class PrivateClass | |
def self.pcall | |
:pcall_result | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
POSSIBLE_VALUES="foo bar baz" | |
TEST_VALUE=$1 | |
echo $POSSIBLE_VALUES | grep $TEST_VALUE 1>/dev/null | |
exit $? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Another example for: http://onor.io/2014/03/31/partial-function-application-in-elixir/ | |
defmodule PartFuncs do | |
defp addfun(x, y), do: x + y | |
# return a partially applied function | |
def add(a), do: &addfun(a, &1) | |
# could also easily be written as: addfun(a, b) | |
def add(a, b), do: (&addfun/2).(a, b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}), |