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
@a2ikm
a2ikm / trace_1.rb
Last active December 10, 2020 07:41
line_tracer = TracePoint.new(:line) do |tp|
p [tp.path, tp.lineno]
end
RSpec.configure do |config|
# enable for each test case
config.around do |example|
line_tracer.enable do
example.run
end
@a2ikm
a2ikm / lsyncd.conf.lua
Last active September 7, 2020 11:06
lsyncd + s3
s3sync = {
maxProcesses = 1,
onStartup = "aws s3 sync ^source ^target",
onCreate = "[ -f ^source^pathname ] && aws s3 cp ^source^pathname ^target^pathname || true",
onModify = "[ -f ^source^pathname ] && aws s3 cp ^source^pathname ^target^pathname || true",
onDelete = "[ -f ^source^pathname ] && aws s3 rm ^target^pathname || true",
onMove = "aws s3 mv ^target^o.pathname ^target^d.pathname",
}
sync {
#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 / patch_migrate.ruby
Last active April 23, 2019 02:49
rake db:migrateでdevelopmentだけでなくtestもまとめてマイグレーション走らせるやつ for Rails 5.1
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 / Gemfile
Last active April 11, 2019 03:10
Use Jbuilder within Sinatra.
source "https://rubygems.org"
gem "sinatra"
gem "tilt-jbuilder", ">= 0.4.0", :require => "sinatra/jbuilder"
gem "hashie"
@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