Skip to content

Instantly share code, notes, and snippets.

def associations_for_includes(recursive: true, except: nil, _breadcrumb: [])
_breadcrumb << self.class_name
self.reflect_on_all_associations.map do |association_reflection|
association_name = association_reflection.name
class_name = association_reflection.class_name
next if _breadcrumb.include?(class_name)
next if association_reflection.options.has_key?(:polymorphic)
case except
@hyoshida
hyoshida / code_hunter.rake
Last active August 29, 2015 14:01
code_hunter を実行し、改善事項がある場合は終了ステータスに 1 を返す
# code_hunter: https://github.com/r7kamura/code_hunter
task :code_hunter do
report_yaml = `code_hunter --application-path=#{Rails.root}`
successed_flag = YAML.load(report_yaml).empty?
puts "\e[31m#{report_yaml}\e[0m" unless successed_flag
exit successed_flag
end
@hyoshida
hyoshida / notes.rake
Last active August 29, 2015 14:02
notesタスクの対象にHACKとXXXを加える
# refs http://crypt.codemancers.com/posts/2013-07-12-redefine-rake-routes-to-add-your-own-custom-tag-in-Rails/
#
# for re-defining the Rake task
# otherwise the previous Rake task is still called
ADDITIONAL_NOTES = %w( hack xxx )
task(:notes).clear
desc "Enumerate all annotations (use notes:optimize, :fixme, :todo, :#{ADDITIONAL_NOTES.join(', :')} for focus)"
task :notes do
# 特異クラスを取得
def eigenclass
class << self; self; end
end
@hyoshida
hyoshida / maybe.rb
Last active August 29, 2015 14:04 — forked from kkismd/maybe.rb
class Maybe < BasicObject
UNPROXIED_METHODS = %i(__send__ __id__ send object_id extend instance_eval initialize block_given? raise caller method)
delegate *(::NilClass.instance_methods - UNPROXIED_METHODS), to: :@just
def initialize(obj)
@just = obj
end
# for debug
@hyoshida
hyoshida / GHOST.c
Last active August 29, 2015 14:14
GHOST exploit
//
// usage:
// gcc -o GHOST GHOST.c
// ./GHOST
//
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@hyoshida
hyoshida / display_category_tree_by_awesome_nested_set.rb
Created March 9, 2015 05:53
Display the category tree by awesome_nested_set
Category.roots.map do |root|
tree = []
root.class.each_with_level(root.self_and_descendants) do |category, level|
tree << "#{' ' * level}#{category.name}"
end
tree
end.join("\n").display
@hyoshida
hyoshida / more_readable_interface.rb
Last active August 29, 2015 14:17
More readable interface in Ruby
# ----------------------------------------------------------------------
#
# I think Java style interface is unreadable in Ruby.
# Because must methods and normal methods are mixed.
#
# Ruby で Java の interface のような実装をすると、
# 普通のメソッドと実装必須なメソッドが混ざってわかりにくい。
#
class Car
def engine
@hyoshida
hyoshida / Makefile
Last active August 29, 2015 14:25
Restart Jenkins from the build of Jenkins
suid: restart_jenkins
sudo chown root restart_jenkins
sudo chmod u+s restart_jenkins
restart_jenkins: restart_jenkins.c
cc restart_jenkins.c -o restart_jenkins
@hyoshida
hyoshida / findup
Created September 13, 2015 09:50
Shell script one-liner to find duplicate files.
find $* -type f | ([ -x "`which sha1sum 2> /dev/null`" ] && xargs sha1sum || xargs shasum) | awk '{ p[$1] = p[$1] ? p[$1] " = " $2 : $2 } END { for (key in p) { if (p[key] ~ / = /) print p[key] } }'