Skip to content

Instantly share code, notes, and snippets.

View ttanimichi's full-sized avatar

Tsukuru Tanimichi ttanimichi

View GitHub Profile
module Kernel
def yield_with_retry(limit: 3, before_retrying: ->{}, before_giving_up: ->{})
limit.times do
begin
return yield
rescue => e
before_retrying.call
end
end
before_giving_up.call
@ttanimichi
ttanimichi / time.js
Last active August 29, 2015 14:21
プロダクションでは moment.js 使ったほうがいいです
'use strict';
class Time {
constructor(time) {
this.time = time;
}
getHours() {
return Math.floor((this.time / 1000 / 3600) % 24);
}
source 'https://rubygems.org'
gem 'rails'
gem 'pg'
group :development, :test do
gem 'pry'
gem 'pry-doc'
gem 'pry-rails'
gem 'pry-byebug'
/.bundle
/.ruby-version
/log/*
!/log/.keep
/tmp
/vendor/bundle
/.env
  • Category has_many topics
  • Topic has_many posts
# app/models/category.rb

class Category < ActiveRecord::Base
  has_many :topics

  validates :name, presence: true, length: { maximum: 255 }
diff --git a/http/app/models/category.rb b/http/app/models/category.rb
index e1f8fac..98869aa 100644
--- a/http/app/models/category.rb
+++ b/http/app/models/category.rb
@@ -1,5 +1,5 @@
class Category < ActiveRecord::Base
- has_many :topics
+ has_many :topics, foreign_key: :category_code
validates :name, presence: true, length: { maximum: 255 }
module ComparingWith
module InstanceVariables
def ==(other)
[self, other].map {|object|
names = object.instance_variables
values = names.map {|name| object.instance_variable_get name }
[names, values].transpose.to_h
}.reduce(&:==)
end
end
@ttanimichi
ttanimichi / require_recursive.md
Last active November 22, 2015 10:16
`require` all files in a directory recursively.

require_recursive

require all files in a directory recursively.

Usage

% gem install require_recursive
require 'singleton'
class MyLogger
include Singleton
def initialize
@logger = $stdout
end
def write(msg)
class Object
def original_class
(self.to_s =~ /#<Class:(\w+)>/) and const_get($1)
end
end
class Foo
class << self
def foo
:foo