Skip to content

Instantly share code, notes, and snippets.

View hrstt's full-sized avatar

sato hiroyuki hrstt

View GitHub Profile
@hrstt
hrstt / csv_to_table.rb
Created June 25, 2011 13:33
For Nanoc, csv data to html table (simple table)
module CSVUtile
## parse csv data to table tag
def csv_to_table(file_name, header = false)
csv = CSV.readlines("data/#{file_name}.csv")
table = ""
table << parse_thead(csv.shift) if header
table << parse_tbody(csv)
end
@hrstt
hrstt / newline_to_br.br
Created June 25, 2011 13:36
exchange line break to <br /> tag
# line break exchange to <br />
def newline_to_br(text)
text = html_escape(text.to_s)
text.gsub!(/\r\n?/, "\n")
text.gsub!(/\n\n+/, "\n")
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />')
end
@hrstt
hrstt / facebook_escape.rb
Created June 25, 2011 13:40
escape url strings for facebook api spesification.
# escape url strings for facebook api spesification.
def facebook_escape(url)
url.gsub('/', '%2F').gsub(':','%3A')
end
@hrstt
hrstt / ItemList.rb
Created June 25, 2011 13:41
For Nanoc, Select specific kind items.
module ItemList
# select specific kind items
# select items by "kind" which is one of item attributes.
# result list sort by created_at
def sorted_list(kind)
@items.select {|i|
i[:kind] == kind
}.sort_by {|a|
if a[:created_at].class == Time then
a[:created_at]
some_array.sort_by {|elem|
[elem[:attribute_a], elem[:attribute_b]]
end
@hrstt
hrstt / .irbrc
Created June 29, 2011 02:09
.irbrc
require 'rubygems' rescue nil
require 'wirble'
require 'hirb'
require 'ap'
# load wirble
Wirble.init
Wirble.colorize
# load hirb
x = { "one" => "one", "two" => "two", "three" => "three"}
y = x.reject {|key,value| key == "three" }
y == { "one" => "one", "two" => "two"}
@hrstt
hrstt / gist:1119133
Created August 1, 2011 22:19
extract duplicate value in array
a = [1,2,3,4,5,6,4,5]
a.inject(Hash.new 0) {|h,v| h[v]+= 1;h}.select {|h,v| v >= 2}.keys #=> [4,5]
@hrstt
hrstt / gist:1175988
Created August 27, 2011 23:16
error brew install io
==> Cloning https://github.com/stevedekorte/io.git
Updating /Users/hsato/Library/Caches/Homebrew/io--git
git remote set-url origin https://github.com/stevedekorte/io.git
git fetch origin
git reset --hard
HEAD is now at c83f98e Merge pull request #129 from jcowgar/master
git checkout-index -a -f --prefix=/private/tmp/homebrew-io-HEAD-VDm0/
==> Downloading patches
==> Patching
/usr/bin/patch -f -p1 -i 001-homebrew.diff
@hrstt
hrstt / ruby_hash_initial_block.rb
Created August 30, 2011 08:05
Ruby: Hash初期化でブロックを利用.各要素に空配列を用意する
hash = Hash.new{|h,k| h[k] = []}
hash[:test] << "Hello" #=> {:test => ["Hello"]}