Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@a2ikm
a2ikm / original.rb
Last active December 11, 2020 11:26
Patching class methods in Ruby
module Document
extend ActiveSupport::Concern
module ClassMethods
def key(name, type)
puts "a new key `#{name}` in `#{type}` is declared."
end
end
end
#include <stdio.h>
int main(int argc, char **argv) {
for (int i = 0; i < 4; i++) {
int j = i;
printf("&i = %p, &j = %p\n", &i, &j);
}
return 0;
}
package main
import "fmt"
func main() {
for i := 0; i < 4; i++ {
j := i
fmt.Printf("&i = %p, &j = %p\n", &i, &j)
}
}
@a2ikm
a2ikm / show-hidden-comments.js
Created April 22, 2020 04:46 — forked from csham/show-hidden-comments.js
Show all hidden comments on github's issue page.
var showHiddenCommentsButtons = document.getElementsByClassName('Details-content--closed');
for (var i=0; i < showHiddenCommentsButtons.length; i++) {
showHiddenCommentsButtons[i].click();
}
@a2ikm
a2ikm / keybindings.json
Last active March 28, 2020 17:46
My keybindings.json for VSCode
// Place your key bindings in this file to override the defaults
[
// cycle tabs in the current group with ctrl+tab and ctrl+shift+tab
{
"key": "ctrl+tab",
"command": "workbench.action.nextEditorInGroup"
},
{
"key": "ctrl+shift+tab",
"command": "workbench.action.previousEditorInGroup"
@a2ikm
a2ikm / string_byteindex2charindex.rb
Created January 18, 2019 12:44
Convert byte index to char index in a String.
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 November 28, 2018 05:29
Usage: ruby reverse_tsv.rb <tsv> [<offset>]
#!/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 October 12, 2018 13:47
nth index of pattern in Ruby's String
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
#!/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 April 18, 2018 06:54
Fiberの1回目のresumeでとりあえずブロック処理に突入させる
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まで進める