Skip to content

Instantly share code, notes, and snippets.

Masato Ikeda a2ikm

Block or report user

Report or block a2ikm

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@a2ikm
a2ikm / string_byteindex2charindex.rb
Created Jan 18, 2019
Convert byte index to char index in a String.
View string_byteindex2charindex.rb
class String
# byte単位で計算されたインデックスを文字単位に変換する。
# 文字の途中や、範囲外の値が渡されたらnilを返す。
#
def byteindex2charindex(byteindex)
return 0 if byteindex == 0
cur = 0
codepoints.each.with_index(1) do |codepoint, index|
cur += codepoint.chr(Encoding::UTF_8).bytesize
@a2ikm
a2ikm / reverse_tsv.rb
Last active Nov 28, 2018
Usage: ruby reverse_tsv.rb <tsv> [<offset>]
View reverse_tsv.rb
#!/usr/bin/env ruby
require "csv"
path = ARGV[0]
offset = ARGV[1] ? ARGV[1].to_i : 1
opts = {}
opts[:col_sep] = "\t" if path.end_with?(".tsv")
rows = CSV.read(ARGV[0], opts)
@a2ikm
a2ikm / nth_index.rb
Created Oct 12, 2018
nth index of pattern in Ruby's String
View nth_index.rb
class String
def nth_index(pattern, n)
pos = nil
offset = nil
begin
pos = index(pattern, offset || 0)
return nil if pos.nil?
n -= 1
offset = pos + 1
end while n >= 0
View git-pushu
#!/bin/sh
remote="origin"
if git remote | grep fork >/dev/null 2>&1; then
remote="fork"
fi
branch="$(git rev-parse --abbrev-ref HEAD)"
git push -u $remote $branch
@a2ikm
a2ikm / fiber_yield.rb
Created Apr 18, 2018
Fiberの1回目のresumeでとりあえずブロック処理に突入させる
View fiber_yield.rb
fib = Fiber.new do
Fiber.yield # y1
a, b = 1, 1
while true
Fiber.yield(a) # y2
a, b = b, a + b
end
end
fib.resume # y1まで進める
@a2ikm
a2ikm / patch_migrate.ruby
Last active Apr 23, 2019
rake db:migrateでdevelopmentだけでなくtestもまとめてマイグレーション走らせるやつ for Rails 5.1
View patch_migrate.ruby
module ActiveRecord::Tasks::DatabaseTasks
class <<self
prepend Module.new {
def migrate(environment = env)
each_current_configuration(environment) { |configuration|
begin
ActiveRecord::Base.establish_connection(configuration)
super()
ensure
ActiveRecord::Base.establish_connection(environment.to_sym)
@a2ikm
a2ikm / tee_capistrano_log.rb
Last active Feb 15, 2018
Tee Capistrano3's logs to stdout and a log file.
View tee_capistrano_log.rb
before :deploy, :setup_tee do
require "tee" # tee gem
# root is where Capfile is placed.
root = File.dirname(File.expand_path(Rake.application.rakefile))
path = File.join(root, "log/deploy_#{release_timestamp}_#{local_user}.log")
FileUtils.mkdir_p(File.dirname(path))
file = File.open(path, "w").tap { |f| f.sync = true }
formatter = SSHKit.config.output
View keybase.md

Keybase proof

I hereby claim:

  • I am a2ikm on github.
  • I am ikm (https://keybase.io/ikm) on keybase.
  • I have a public key ASC-htoOrKqZHAobMtu_RW0WbO4LrECHegJ6ZApDr6-uuwo

To claim this, I am signing this object:

@a2ikm
a2ikm / USAGE
Last active Dec 21, 2017
Log deprecated methods in Rails app
View USAGE
__deprecate def old_method(a, b, &bkock)
:
end
attr_accessor :old_accessor
__deprecate_accessor :old_accessor
attr_reader :old_reader
__deprecate_reader :old_reader
View EchoServer.swift
import Foundation
let GlobalQueue = DispatchQueue(label: "st.aerial.Yukon.queue")
class EchoServer {
// let localQueue = dispatch_queue_create("\(EchoServer.self)",
// DISPATCH_QUEUE_CONCURRENT)
var serverSocket: Int32
var clientsAccepted: [Int32 : DispatchSourceRead]
You can’t perform that action at this time.