Skip to content

Instantly share code, notes, and snippets.

# Check JAN(Japanse Area Number) is valid or not
class JanCheck
def self.valid?(number)
new(number).valid?
end
attr_reader :number
def initialize(number)
@number = number.to_s
@libkazz
libkazz / gist:5433115
Created April 22, 2013 07:46
1行で使用しているポートと使っているプロセス名を出す
sudo netstat -ntulp | grep -v '127.0.0.1:' | egrep '(tcp|udp)' | ruby -anle 'print $_ + "\t" + open("/proc/#{$_.match(/ (\d+)\//)[1]}/cmdline", "r").gets'
@libkazz
libkazz / gist:5433153
Created April 22, 2013 07:59
OpenOffice で作った表形式のデータを手軽に扱う
# Usage:
# Book.new('foo.ods').sheet('first sheet').each do |row|
# puts "first column => " + row["first column"]
# puts "second column => " + row["second column"]
# end
#
# Write gem 'roo' at Gemfile
module Foo
class Book
def initialize(file)
@libkazz
libkazz / gist:7113677
Created October 23, 2013 06:47
rails の migration で id(primary key) をつける。しかも、partitioning を設定してるテーブルに。
class AddColumnIdToLogs < ActiveRecord::Migration
def up
add_column :logs, :id, :integer, null: false # FIXME: Add column to head of columns
execute 'SET @rownum = 0'
execute 'UPDATE `access_logs` SET `id` = @rownum := @rownum + 1'
execute 'ALTER TABLE `logs` ADD PRIMARY KEY(`id`, `date`)' # Partitioning key () must be primary key.
execute 'ALTER TABLE `logs` CHANGE `id` id INT(11) AUTO_INCREMENT'
end
@libkazz
libkazz / hash_combination.rb
Created May 31, 2016 08:16
Hash#combination
vals = {
type: ['wood','iron','other'],
length: [1,2,3,4,5],
width: [1,2,3],
}
class Hash
def combination
keys.reduce([{}]) do |r, key|
r = _add_to_combination(r, key)
@libkazz
libkazz / out.sh
Created July 7, 2016 09:39
sitemap.xml の URL 一覧を出力する
gzcat tmp/sitemaps/sitemap.xml.gz | ruby -rnokogiri -e 'Nokogiri::XML($stdin.read).search("url/loc").each {|n| puts n.text }'
@libkazz
libkazz / count_front_css_lines.rb
Last active November 5, 2016 07:07
CSS 行数推移チェック
since = '2016/3/22'
path = 'app/assets/stylesheets'
`git log --pretty="format:%ad %h" --date=short --since="#{since}" #{path}`.split("\n").reverse.each do |line|
date, rev = line.split
system("git checkout #{rev} 2> /dev/null")
count = `find #{path} -type f -exec wc -l {} \\; | awk '{n+=$1} END{print n}'`.chomp.to_i
puts [date, rev, count].join(',')
end
@libkazz
libkazz / stone_tori.rb
Created January 31, 2017 15:59
石取りゲーム
class StoneTori
class Stones
def initialize(count)
@count = count
end
def empty?
@count.zero?
end
@libkazz
libkazz / gist:99e1d91a664d490d79ad85a25137094f
Created November 2, 2017 16:17 — forked from liamcurry/gist:2597326
Vanilla JS vs jQuery

Moving from jQuery

Events

// jQuery
$(document).ready(function() {
  // code
})
@libkazz
libkazz / toggle_colspanned_column.js
Created December 9, 2017 11:04
colspan = 2 のカラムの toggle する
console.log('Loaded..');
class Togglable {
constructor(th) {
this.th = th;
this.table = th.parentNode.parentNode.parentNode;
this.tds = this.table.querySelectorAll('td:nth-child(' + this._getColumnIndex(th) + ')')
}
toggle() {