- @hamajyotan (SAKAGUCHI Takashi)
- office ummm
- rubyist
- slowlife engineer (farmer, programmer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/hoge.rb | |
class Hoge < ApplicationRecord | |
validates :foo, presence: true | |
validates :bar, presence: true | |
validates :foo, uniqueness: { scope: [:bar] } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# NOT NULL 制約の定義されている列に対し、自動的に presence バリデーションを付与します | |
# | |
module AutoPresenceValidation | |
extend ActiveSupport::Concern | |
included do | |
ignore_types = %i[boolean] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f *args | |
args.map(&:to_s).permutation.map { |x| x.join.to_i }.max | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record/relation' | |
# Usage | |
# | |
# class User | |
# scope :foo, -> { where(name: 'foo') } | |
# scope :bar, -> { where(name: 'foo') } | |
# end | |
# | |
# > User.foo.or User.bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Usage: ./hoge KEYWORD | |
# | |
# ./hoge Java | |
# => | |
# 名前 Java<br/> パラダイム 構造化プログラミング 構造化・命令型プログラミング... | |
# | |
require 'net/http' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
class Hoge | |
def moe | |
`ls -l` | |
end | |
end | |
describe "hoge" do | |
let(:hoge) { Hoge.new } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
NewerOlder