Skip to content

Instantly share code, notes, and snippets.

# Search who cause the test function succeed from candidates
#
# @param [Array<T>] array of candidates
# @param [(Array<T>) -> bool] lambda function to test subset of candidates
# @return [T] last survivor against test function
def bisection_search(candidates, test)
loop do
head = candidates[0...candidates.length/2]
tail = candidates - head
@a2ikm
a2ikm / psqldef_export.sh
Created March 17, 2024 07:37
How to run psqldef --export against your heroku PostgreSQL
# on your local machine
heroku run bash
# on one-off dyno
wget -O - https://github.com/sqldef/sqldef/releases/latest/download/psqldef_linux_amd64.tar.gz | tar xvz
./psqldef --user ${USERNAME_FROM_DATABASE_URL} --password-prompt --host ${HOSTNAME_FROM_DATABASE_URL} --export ${DATABASE_NAME_FROM_DATABASE_URL}
Enter Password:${PASSWORD_FROM_DATABASE_URL}
#!/usr/bin/env ruby
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "mongo_mapper"
end
# You can boot MongoDB with Docker:
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
@a2ikm
a2ikm / randrop2delcious.rb
Created August 21, 2022 18:44
Convert CSV exported from Raindrop to Delcious XML format
#!/usr/bin/env ruby
# c.f. https://github.com/domainersuitedev/delicious-api/blob/master/api/posts.md#v1postsall
#
# <posts tag="" user="user">
# <post href="http://www.weather.com/" description="weather.com"
# hash="6cfedbe75f413c56b6ce79e6fa102aba" tag="weather reference"
# time="2005-11-29T20:30:47Z" />
# ...
# <post href="http://www.nytimes.com/"
#!/usr/bin/env ruby
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "activesupport", "~> #{ENV["AS_VERSION"]}"
end
require "active_support"
@a2ikm
a2ikm / patching_included_module.rb
Created January 12, 2022 15:59
ruby 3.0 から include したモジュールに対する変更が以前に include したモジュールにも反映されるようになったので、 ruby >= 3.0 ならそのモジュールに対して prepend すればオーバーライドできる。それ以前はそのモジュールを module_eval するなどして直接書き換える必要がある
module Mod
module A
def message
"A"
end
end
module B
def message
"B#{super}B"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
@a2ikm
a2ikm / main.go
Created August 9, 2021 15:43
go html/template sandbox
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{.Header.Title}}</title>
</head>
<body>
<h1>{{.Header.Title}}</h1>
@a2ikm
a2ikm / README.md
Last active December 15, 2020 06:02
Replace included module with parser gem
$ cat mongo_mapper.rb
$ cat mongo_mapper.rb | ruby rewrite.rb
class Foo
  include Mongoid::Document

  key :name, String
end