Skip to content

Instantly share code, notes, and snippets.

@shuuuuun
shuuuuun / table_printer.rb
Last active March 18, 2023 09:14
さくっとhashをテーブル出力したいときに使うやつ
class TablePrinter
# Initialize data
# @param data [Array] printing data as array of hash.
# @param labels [Hash] column labels hash.
# @example
# data = [
# { name: "foo", installed_versions: ["5.15.5_3"], current_version: "5.15.7" },
# { name: "bar", installed_versions: ["7.0.4"], current_version: "7.0.5" },
# { name: "baz", installed_versions: ["3.2.5"], current_version: "3.2.7" },
# ]
@shuuuuun
shuuuuun / puma_start
Last active December 8, 2021 15:41
[pumactl.rb]
#!/usr/bin/env ruby
APP_ROOT = File.expand_path "#{File.dirname(__FILE__)}/.."
pid_file = "#{APP_ROOT}/tmp/pids/app.pid"
if File.exist?(pid_file)
puts 'Found puma pid file!'
puts 'Puma may have been already started!'
exit
end
@shuuuuun
shuuuuun / promise-memo.js
Last active October 14, 2020 01:13
promise-memo.js
// 並列実行
;(async () => {
const sleepTimes = [1000, 2000, 3000]
// この時点で実行開始される
const promises = sleepTimes.map((num) => new Promise((resolve, _reject) => {
setTimeout(() => {
const now = new Date()
console.log(`${num} msec later. ${now}`)
resolve(now)
# ref. https://railsguides.jp/caching_with_rails.html
# ref. https://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html
# write/read/fetch
Rails.cache.write('test_cache_key', Time.zone.now, expires_in: 10.seconds)
Rails.cache.read('test_cache_key', expires_in: 10.seconds)
Rails.cache.fetch('test_cache_key', expires_in: 10.seconds) { Time.zone.now }
class Model
def self.hoge
@shuuuuun
shuuuuun / test_slack_notifier.rb
Created April 1, 2020 12:38
slack通知テストしたときのメモ
require 'uri'
require 'net/http'
module TestSlackNotifier
extend self
def send
uri = URI.parse('https://slack.com/api/chat.postMessage')
params = {
channel: '#channel_name',
# headless chromeでスクリーンショットを撮るやつ
# ref. https://developers.google.com/web/updates/2017/04/headless-chrome?hl=ja
alias chrome='/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
# shot!
chrome --headless --disable-gpu --screenshot="./screenshot/hoge.png" --window-size=1280,4000 https://example.com/hoge
# shot!shot!shot!
pages=(hoge fuga piyo)
@shuuuuun
shuuuuun / transcribe-speaker.rb
Created January 22, 2020 14:40
Amazon Transcribe で出力されたjsonファイルから話者ごとのセンテンスを抽出してtxtファイルに保存
#!/usr/bin/env ruby
# Amazon Transcribe で出力されたjsonファイルから話者ごとのセンテンスを抽出してtxtファイルに保存
require 'json'
def convert(input_file, output_file)
input = JSON.parse(File.read(input_file))
results = input['results']
@shuuuuun
shuuuuun / ruby-file-memo.rb
Last active March 21, 2021 01:48
rubyのFileまわりで忘れがちなやつメモ
#!/usr/bin/env ruby
puts "__FILE__: #{__FILE__}"
puts "__dir__: #{__dir__}"
puts "File.dirname(__FILE__): #{File.dirname(__FILE__)}"
puts "File.expand_path('.', __FILE__): #{File.expand_path('.', __FILE__)}"
puts "File.expand_path('.', __dir__): #{File.expand_path('.', __dir__)}"
# スクリプトが1階層下にある場合
APP_ROOT = File.expand_path('..', __dir__)
100.times do |i|
str = ''
str += 'Fizz' if i % 3 == 0
str += 'Buzz' if i % 5 == 0
str += i.to_s if str == ''
puts str
end
100.times do |i|
print 'Fizz' if i % 3 == 0
# usage:
# 1. Install aws-sdk
# $ echo "source 'https://rubygems.org'\ngem 'aws-sdk'" > Gemfile
# $ bundle install --path vendor/bundle
# # or
# $ gem install aws-sdk
# 2. Setup AWS credentials
# $ eval $(assume-role hoge)
# # or
# $ export AWS_PROFILE=hoge