Skip to content

Instantly share code, notes, and snippets.

@nuna
nuna / nintendo.log
Created March 11, 2012 14:08
任天堂採用情報ページの参考課題
$ time curl -Lso - http://bit.ly/ye8vZA | zcat | ruby -rtext -e 'w=ARGF.read.split(/\n/);p %w(インターネト プログラミングル 東強都 任人堂 東京特許許許可局 マロオブラザーズ イーサネッット コソトローラ).map{|i|w.sort_by{|j|Text::Levenshtein.distance(i,j)}[0]}'
["インターネット", "プログラミング", "東都", "任天堂", "東京特許許可局", "マリオブラザーズ", "イーサネット", "コントローラ"]
real 14m13.030s
user 13m28.858s
sys 0m5.232s
@nuna
nuna / factories.rb
Created February 16, 2012 11:39
factory_girl + Rails 3.2.1 / legacy fanctional test
class FactoryGirl::TestHelper
include ActionDispatch::TestProcess
def self.fixture_file_upload(*args)
self.new.fixture_file_upload(*args)
end
def self.fixture_path
File.join(File.dirname(__FILE__) + '/fixtures/')
end
@nuna
nuna / rename.rb
Created November 25, 2011 02:37
指定したディレクトリ以下の特定のファイルを再帰的にリネーム
#!/usr/bin/ruby
require 'find'
require 'fileutils'
dir = ARGV.shift
Find.find(dir) do |f|
if File.file?(f) and /20111123.*\.xls$/ =~ f
FileUtils.mv(f, f.sub(/20111123/, '20111124'), { :noop => false, :verbose => true })
@nuna
nuna / file rename with ruby
Created November 24, 2011 12:15
カレントディレクトリ以下のthumnailとミススペリングしているファイルをthumbnailに直す
# まず dry run
$ find . -name thumnail.png | ruby -rfileutils -ne '$_.chomp!; FileUtils.mv($_, $_.sub(/thum/, "thumb"), {:noop => true, :verbose => true})'
# 本当に実行
$ find . -name thumnail.png | ruby -rfileutils -ne '$_.chomp!; FileUtils.mv($_, $_.sub(/thum/, "thumb"), {:noop => false, :verbose => false})'
@nuna
nuna / redefine_string_plus
Created November 8, 2011 10:45
redefine String#+
$ irb
ruby-1.8.7-p352 :001 > str = 'a'
=> "a"
ruby-1.8.7-p352 :002 > str + 'b'
=> "ab"
ruby-1.8.7-p352 :003 > p str
"a"
=> nil
ruby-1.8.7-p352 :004 > class String
ruby-1.8.7-p352 :005?> def +(other)
@nuna
nuna / gist:1326789
Created October 31, 2011 02:40
open png file as ascii-bit(binary)
$ irb-ruby-1.9.2-p290
ruby-1.9.2-p290 :001 > RUBY_VERSION
=> "1.9.2"
ruby-1.9.2-p290 :002 > File.read('test.png').encoding
=> #<Encoding:UTF-8>
ruby-1.9.2-p290 :003 > image = File.open('test.png', 'r:ascii-8bit'){|f| f.read}
<snip>
ruby-1.9.2-p290 :004 > image.encoding
=> #<Encoding:ASCII-8BIT>
@nuna
nuna / gist:1322057
Created October 28, 2011 10:48
base64 encode/decode
$ file test.png
test.png: PNG image data, 22 x 13, 16-bit grayscale, non-interlaced
$ md5sum test.png
c6db60c696981f908324d3365e0836c0 test.png
# base64 encoding with ruby 1.8.7
$ irb-ruby-1.8.7-p334
ruby-1.8.7-p334 :001 > RUBY_VERSION
=> "1.8.7"
ruby-1.8.7-p334 :002 > b64 = [File.read('test.png')].pack('m')
@nuna
nuna / struct and array sample code
Created October 21, 2011 04:34
Array of Struct object
#!/usr/bin/ruby
Person = Struct.new(:name, :age)
jo = Person.new("Jo", 15)
meg = Person.new("Meg", 16)
beth = Person.new("Beth", 10)
amy = Person.new("Amy", 7)
sisters = [jo, meg, beth, amy]
sisters.each{|person| puts person.name }
#=> Jo
@nuna
nuna / 009_no-gems.patch
Created August 24, 2011 15:41
patch for ruby-1.9.2-p290
Index: ruby-1.9.2-p290/tool/rbinstall.rb
===================================================================
--- ruby-1.9.2-p290.orig/tool/rbinstall.rb
+++ ruby-1.9.2-p290/tool/rbinstall.rb
@@ -491,38 +491,6 @@
end
end
-install?(:ext, :comm, :gem) do
- directories = []
@nuna
nuna / summize-traffic.rb
Created December 22, 2010 01:42
Calculate bytes of the traffic of each domain using mongodb map/reduce.
#!/usr/bin/ruby19
# -*- coding: utf-8 -*-
require 'mongo'
connection = Mongo::Connection.new
db = connection.db('apache-log')
logs = db.collection('log')
map =<<'_FUNC_'