Skip to content

Instantly share code, notes, and snippets.

@enil
enil / last.rb
Last active December 18, 2015 20:59
A recursive method to return the last parameter, Haskell-style.
def last(first=nil, *rest)
if rest.empty?
first
else
last(*rest)
end
end
@enil
enil / unfuck.rb
Created July 4, 2013 17:04
The way arrays are used as an variable argument list, *unless* the arity of a proc is 1 is a clusterfuck. I think this is a safe way to fix a proc so that sending an array to the proc always treats the array as an argument list. `BasicObject.instance_exec` doesn't seem to use `Proc.call`, it has to be dealt with separately.
class Proc
def unfuck!
class << self
def call(*params)
if arity == 1 && params.first.is_a?(Array)
super(params.first.first)
else
super
end
end
class Symbol
# Makes :symbol.(a,b,c) equal to symbol(a,b,c).
def call(*params, &block)
send(self, *params, &block)
end
end
public class ListTransformation {
private ListTransformation() { /* empty */ }
public static <T> List<T> collect(List<T> inputList, Predicate<T> predicate) {
List<T> newList = new ArrayList(inputList);
Iterator<T> iterator = newList.iterator();
while (iterator.hasNext()) {
try {
if (!predicate.check(iterator.next())) {
Class.metaClass.getDimensions = { -> isArray() ? 1 + componentType.dimensions : 0 }
@enil
enil / linkrb
Created May 17, 2015 14:51
Creates links for Ruby 2.2 without the prefix (not needed)
#!/bin/sh
package=ruby22
suffix=2.2
for source in $(port contents $package | grep /opt/local/bin/); do
target=$(dirname $source)/$(basename $source $suffix)
ln -s $source $target
done
@enil
enil / wait
Created June 5, 2015 17:00
Wait for user to press any key in ZSH
#!/bin/zsh
echo 'Press any key to continue...'; read -k1 -s
@enil
enil / bool.rb
Created June 20, 2015 15:15
A bit of Ruby sugar for boolean values
class Object
def to_bool
!!self
end
alias true? to_bool
def false?
!true?
end
@enil
enil / curldocker.zsh
Last active August 29, 2015 14:24
Easily curl into the Docker REST API (ZSH only)
curldocker() {
local args
local endpoint
args=(${@:1:$(( $# - 1 ))})
endpoint=${@[$#]}
curl --silent \
--key $DOCKER_CERT_PATH/key.pem \
--cert $DOCKER_CERT_PATH/cert.pem \
ENV['DOCKER_HOST'].sub(%r{(?<=tcp://)[^:]+}) { |ip| Resolv.getname(ip) }