Skip to content

Instantly share code, notes, and snippets.

View hamajyotan's full-sized avatar

SAKAGUCHI Takashi hamajyotan

View GitHub Profile
@hamajyotan
hamajyotan / extconf.rb
Created December 6, 2011 10:56
Data_Wrap_Struct example
require "mkmf"
$libs += " -lstdc++ "
create_makefile("human")
@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 / 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 / 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 / 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]
@hamajyotan
hamajyotan / bootstrap.js
Created October 9, 2012 13:21
rails bootstrap script for 3.2
/* ===================================================
* bootstrap-transition.js v2.1.1
* http://twitter.github.com/bootstrap/javascript.html#transitions
* ===================================================
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
@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