Skip to content

Instantly share code, notes, and snippets.

View yancya's full-sized avatar
💭
😂

Shinta Koyanagi yancya

💭
😂
View GitHub Profile
-- psql -f create_db.sql postgres
CREATE DATABASE hogehoge;
@yancya
yancya / composer.json
Last active June 26, 2020 13:45
謎の HAML ライブラリを使う方法
{
"name": "yancya/php_sandbox",
"authors": [
{
"name": "yancya",
"email": "yancya@upec.jp"
}
],
"require": {
"mthaml/mthaml": "*"
ROOMS = ['a', 'b']
rooms_order = ROOMS.map.with_index { |r, i| "WHEN '#{r}' THEN #{i} " }.join.yield_self { |s| "CASE room #{s} END"}
Topic.where(room: ROOMS).order(rooms_order)
#=> SELECT "topics".* FROM "topics" WHERE "topics"."room" IN ('a', 'b') ORDER BY CASE room WHEN 'a' THEN 0 WHEN 'b' THEN 1 END
ROOMS = ['a', 'b']
rooms_table = ROOMS.map.with_index { |r, i| "('#{r}',#{i})" }.join(',').yield_self { |s| "(VALUES#{s}) as rooms(room, room_order)" }
Topic.joins("join #{rooms_table} using(room)").order(:room_order)
#=> SELECT "topics".* FROM "topics" join (VALUES('a',0),('b',1)) as rooms(room, room_order) using(room) ORDER BY "room_order" ASC
require 'pg'
require 'tapp'
def main(str)
line_segments = str.split('/').flat_map { |sq|
left, top, right, bottom = sq.chars.map { |i36| i36.to_i(36) }
[ # ax ay bx by
[left, top, right, top ], # top segment
[right, top, right, bottom], # right segment
[left, bottom, right, bottom], # bottom segment
CREATE TEMP TABLE "employees" (
"emp_code" text,
"name" text,
"valid_from" timestamp
);
INSERT INTO "employees" VALUES
('001', 'Jane', '2019-01-10'),
('001', 'Tom', '2019-01-15'),
('001', 'Kevin', '2019-01-20');
class Array
def +@
false
end
end
class TrueClass
def +(o)
to_s + o.join(',')
end
@yancya
yancya / Dockerfile
Created June 18, 2018 11:58
bundle install 時に rake が見つからなくてコケる例
FROM ruby
RUN apt-get update && apt-get install -y libpoppler-dev
WORKDIR /var/app
COPY Gemfile .
RUN bundle # こける
# `RUN gem install poppler && bundle` だと成功する
create temp table hoge(id integer, name text);
with hoge_candidate(id, name) AS (values(1, 'hoge'))
, inserted_hoge AS (
insert into hoge
select hoge_candidate.*
from hoge right outer join hoge_candidate using(id)
where hoge.id is null returning *)
select * from inserted_hoge