Skip to content

Instantly share code, notes, and snippets.

View ar_test.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
@sinsoku
sinsoku / application_controller.rb
Created Oct 8, 2020
before/after/around actionsの前後に処理を挟むパッチ
View application_controller.rb
class ApplicationController < ActionController::Base
class << self
[:before, :after, :around].each do |callback|
define_method "#{callback}_action" do |*names, &blk|
options = names.extract_options!
new_blk = -> {
# 本来の処理の前に行う処理を書く。
if blk.is_a?(Proc)
blk.call
@sinsoku
sinsoku / Dockerfile
Created Aug 16, 2020
An image to edit rails credentials
View Dockerfile
# build:
# $ docker build -t rails_cred .
#
# usage:
# $ docker run -it --rm \
# -e RAILS_MASTER_KEY=$RAILS_MASTER_KEY \
# -v `pwd`/config/credentials.yml.enc:/app/config/credentials.yml.enc \
# -v `pwd`/config/credentials:/app/config/credentials \
# rails_cred
View oop.rb
# bad
class A
def cool_calc(b)
b.foo + b.bar * 2
end
end
# good
class B
def cool_value
View enum.rb
enum = Enum.new(:a, :b, :c)
status = :b
# pattern 1
enum.case status do
_when(:a) do
p :a
end
_when :b do
@sinsoku
sinsoku / case_match.rb
Last active Jun 23, 2020
A gem for finding non-exhaustive case statements.
View case_match.rb
module CaseMatch
class Error < StandardError; end
class Case
EMPTY_BLOCK = -> {}
def initialize(obj, cases)
@obj = obj
@cases = cases
@values = []
@sinsoku
sinsoku / enumerator_pipeline.rb
Last active May 7, 2020
`map { |x| bar(foo(x)) }` to `map > :foo >> :bar`
View enumerator_pipeline.rb
require 'binding_of_caller'
module EnumeratorPipeline
class Builder
def initialize(name)
@callbacks = []
@callbacks << binding.of_caller(1).method(name)
end
def >>(name)
@sinsoku
sinsoku / main.rs
Created Mar 26, 2020
I cannot build main.rs that includes `Option<R: BufRead>`. orz
View main.rs
use std::io::{self, Empty, BufRead, Write, Cursor};
fn read_input<R, W>(source: Option<R>, mut writer: W) -> String
where R: BufRead, W: Write
{
write!(&mut writer, "input: ").unwrap();
writer.flush().unwrap();
let mut input = String::new();
match source {
View state_sample.rb
# app/models/article.rb
class Article < ApplicationRecord
has_many :article_publications
scope :draft, -> { left_joins(:article_publications).where(article_publications: { id: nil }) }
scope :published, -> { joins(:article_publications) }
end
# app/models/article_publication.rb
class ArticlePublication < ApplicationRecord
View ar_test.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"