View toy.rb
class Array
def to_proc
->(this) { this.send(*self) }
end
end
[1,2,3].map(&[:+, 1]) #=> [2,3,4]
def apply(sym, args=[]); ->(this) { this.send(sym, *args) }; end
alias _ apply
View find-max-key-in-condition.rb
require 'benchmark'
require 'active_support/all'
n = ARGV[0]&.to_i || 50000
m = ARGV[1]&.to_i || 20
arr = Array.new(m){|i| [i, [:a, :b, :c, :d].sample] }.shuffle.to_h
Benchmark.bmbm do |b|
b.report("1:") { n.times{ arr.select{|_,v| v == :a }.max&.first }}
b.report("2:") { n.times{ arr.each_with_object([]){|(k,v),o| o << k if v == :a }.max }}
View hash-find-max-bench.rb
require 'benchmark'
require 'active_support/all'
n = 20000
m = 100
arr = Array.new(m){|i| [i, [:a, :b, :c, :d].sample] }.shuffle.to_h
Benchmark.bmbm do |b|
b.report("1:") { n.times{ arr.select{|_,v| v == :a }.max&.first }}
b.report("2:") { n.times{ arr.each_with_object([]){|(k,v),o| o << k if v == :a }.max }}
View table-data-gateway.md

Table Data Gateway

多くの開発者はSQLをうまく書けるわけではないから、DBを簡単に操作するインタフェースを Table Data Gateway で用意する。

# ex.
# Items
#   id integer primary key
View iap-trial.rb
require 'sinatra'
require 'jwt'
require 'json'
def get_iap_key(kid)
@key_table ||= begin
require 'open-uri'
resp = open('https://www.gstatic.com/iap/verify/public_key', &:read)
JSON.parse(resp)
end
View hello.rb
$><<(%<><<(%$%(/</<=)<%~~~/%>>>~<>)/>>)=)/(>%()<>~~~()<=%/=<=>)=(%=<~>(~/><<~)//)</)=$=~/$/)<<(%$<)>><%(>/()(//(=>%~<=((<=((=%/%~(((>//=<=)/=/<>(>~~<)~>~/~=~)~==%)><%=<=~(=/~//>)><%<>((<)/~)<<=>/%=<$=~/$/)<<(%$</)>(=%))%(<)=></<)~<<))/~/<<~/)//=/>~%~)((()>)/=(%)>>~/~<<()(((()<)~>%<)%%~~<%><==(()((/((><%%=%)=<%%=(%~~)$=~/$/)<<(%$=>~~>>/>>><)=<>=~=<(/%>(~%~()(~<>%)()>)(=<%)=%<>()(%==%<>/(>%~=<//%/(~)/<(%(><)>(>=%)>/~(%><=>)>~>))(<<((~%>$=~/$/)<<(%$</=(~>%~~><=~=<))/~()/(~=%(=<()/=/(~)~~~=/><)<<>>>~///=>))=(>>/(())<>/%<<<==/%(<~<~>/(%<()(/=%=))~%~(/%>==)/~>/$=~/$/))
View bq_query.rb
# https://github.com/google/google-api-ruby-client/blob/master/samples/cli/lib/samples/bigquery.rb
require 'google/apis/bigquery_v2'
require 'googleauth'
require 'securerandom'
# CHANGEME
project_id = 'myproject'
dataset_id = 'mydataset'
table_id = 'mytable'
View ltsv2csv
#!/usr/bin/env ruby
require 'csv'
firstline = true
io = $stdout
while line = ARGF.gets
data = line.chomp.split(/\t/).map{|item| item.split(/:/,2) }.to_h
if firstline
View slacksay.sh
#!/bin/sh
# set ft=sh
set -e
# available variables:
#
# SLACKSAY_WEBHOOKURL
# SLACKSAY_CHANNEL
# SLACKSAY_USERNAME
View skype.sh
#!/bin/sh
rm -rf "~/Library/Application Support/Skype"
open "/Applications/Skype.app"