Skip to content

Instantly share code, notes, and snippets.

View holysugar's full-sized avatar

HORII Keima holysugar

  • Aiming, Inc
  • Tokyo
View GitHub Profile
@holysugar
holysugar / toy.rb
Last active October 30, 2017 07:33
to_proc
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
@holysugar
holysugar / find-max-key-in-condition.rb
Last active October 10, 2017 09:33
today's bench
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 }}
@holysugar
holysugar / hash-find-max-bench.rb
Created October 10, 2017 09:18
today's bench
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 }}
@holysugar
holysugar / table-data-gateway.md
Created August 24, 2017 07:00
poeaa, 10-1, table data gateway

Table Data Gateway

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

# ex.
# Items
# id integer primary key
@holysugar
holysugar / iap-trial.rb
Last active July 12, 2017 05:48
identity-aware proxy (IAP) trial code
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
@holysugar
holysugar / hello.rb
Created March 15, 2017 12:27
print "Hello"
$><<(%<><<(%$%(/</<=)<%~~~/%>>>~<>)/>>)=)/(>%()<>~~~()<=%/=<=>)=(%=<~>(~/><<~)//)</)=$=~/$/)<<(%$<)>><%(>/()(//(=>%~<=((<=((=%/%~(((>//=<=)/=/<>(>~~<)~>~/~=~)~==%)><%=<=~(=/~//>)><%<>((<)/~)<<=>/%=<$=~/$/)<<(%$</)>(=%))%(<)=></<)~<<))/~/<<~/)//=/>~%~)((()>)/=(%)>>~/~<<()(((()<)~>%<)%%~~<%><==(()((/((><%%=%)=<%%=(%~~)$=~/$/)<<(%$=>~~>>/>>><)=<>=~=<(/%>(~%~()(~<>%)()>)(=<%)=%<>()(%==%<>/(>%~=<//%/(~)/<(%(><)>(>=%)>/~(%><=>)>~>))(<<((~%>$=~/$/)<<(%$</=(~>%~~><=~=<))/~()/(~=%(=<()/=/(~)~~~=/><)<<>>>~///=>))=(>>/(())<>/%<<<==/%(<~<~>/(%<()(/=%=))~%~(/%>==)/~>/$=~/$/))
@holysugar
holysugar / bq_query.rb
Last active November 15, 2016 14:43
bigquery v2 call sample (クエリのテーブルへの出力)
# 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'
@holysugar
holysugar / ticketLink
Created February 16, 2012 13:26
Google SpreadSheets Auto Link Script
@holysugar
holysugar / protobahamuto.rb
Last active August 22, 2016 21:15
gbf proto-bahamuto call notification
require 'slack-notifier'
require 'tweetstream'
SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'
TweetStream.configure do |config|
config.consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
config.consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
config.oauth_token = 'XXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
@holysugar
holysugar / ltsv2csv
Created August 8, 2016 05:17
from ltsv to csv converter
#!/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