Skip to content

Instantly share code, notes, and snippets.

View hamajyotan's full-sized avatar

Takashi Sakaguchi hamajyotan

View GitHub Profile
@hamajyotan
hamajyotan / hoge.rb
Created May 24, 2019 00:06
activerecord validation error i18n
# app/models/hoge.rb
class Hoge < ApplicationRecord
validates :foo, presence: true
validates :bar, presence: true
validates :foo, uniqueness: { scope: [:bar] }
end
@hamajyotan
hamajyotan / auto_presence_validation.rb
Created September 13, 2017 00:31
NOT NULL 制約の定義されている列に対し、自動的に presence バリデーションを付与
# frozen_string_literal: true
# NOT NULL 制約の定義されている列に対し、自動的に presence バリデーションを付与します
#
module AutoPresenceValidation
extend ActiveSupport::Concern
included do
ignore_types = %i[boolean]
@hamajyotan
hamajyotan / ruby2.3.md
Created February 13, 2016 14:05
第21回鳥取Ruby会とっとるびー勉強会資料

Ruby 2.3 変更点とか

p self

  • @hamajyotan (SAKAGUCHI Takashi)
  • office ummm
  • rubyist
  • slowlife engineer (farmer, programmer)

changelog

@hamajyotan
hamajyotan / 0sim
Last active September 30, 2016 13:53
So-net 0sim
#!/usr/bin/env ruby
require 'bundler'
Bundler.require
Capybara.run_server = false
Capybara::Session.new(:poltergeist).tap do |page|
page.visit 'https://www.so-net.ne.jp/retail/w/'
page.within 'form[name=Login]' do
page.find('input[name=IDToken1]').set ENV['ZEROSIM_ID']
@hamajyotan
hamajyotan / q4
Created May 22, 2015 18:22
Five programming problems every Software Engineer should be able to solve in less than 1 hour
def f *args
args.map(&:to_s).permutation.map { |x| x.join.to_i }.max
end
@hamajyotan
hamajyotan / with_retry.rb
Created March 25, 2015 14:01
block with retry
def with_retry times: 2, catch: StandardError
yield binding.local_variable_defined?(:tries) ? binding.local_variable_get(:tries).to_i : 0
rescue *catch
raise unless (tries = tries.to_i + 1) < times
retry
end
# or
def with_retry times: 2, catch: StandardError
@hamajyotan
hamajyotan / reduce_by_or.rb
Created November 13, 2014 00:50
ActiveRecord::Relation reduce by 'OR'
require 'active_record/relation'
# Usage
#
# class User
# scope :foo, -> { where(name: 'foo') }
# scope :bar, -> { where(name: 'foo') }
# end
#
# > User.foo.or User.bar
@hamajyotan
hamajyotan / hoge
Created October 23, 2014 15:41
get wikipedia content by wikipedia api
#!/usr/bin/env ruby
# Usage: ./hoge KEYWORD
#
# ./hoge Java
# =>
# 名前 Java<br/> パラダイム 構造化プログラミング 構造化・命令型プログラミング...
#
require 'net/http'
@hamajyotan
hamajyotan / gist:6681517
Last active December 23, 2015 19:19
バッククォートでのシステムコマンド実行が呼び出されることを RSpec で検証する 追記: $? の値が任意であることを検証する方法を追加
# -*- encoding: utf-8 -*-
class Hoge
def moe
`ls -l`
end
end
describe "hoge" do
let(:hoge) { Hoge.new }
# Dollar class.
#
# d1 = Dollar.new 12345 # => $ 12,345.00
# d1.dollar # => 12345
# d2 = Dollar.new 1234, 567 # => $ 1,239.67
# d2.cent # => 67
# d3 = Dollar.new 1239, 67 # => $ 1,239.67
# d2 == d3 # => true
# d4 = Dollar.new 12345, 2 # => $ 12,345.02
# (d1..d4).to_a # => [$ 12,345.00, $ 12,345.01, $ 12,345.02]