require 'tempfile' class Uri # def self.escape_raw(st) st.gsub(/([^a-zA-Z0-9_\-\.~])/) { "%#{$1.unpack('H*')[0].scan(/../).join('%').upcase }" } end # # usage # @search_tag = Uri.unescape_raw(params[:tag]).toutf8 # # def self.unescape_raw(st) st.tr('+',' ').gsub(/%([A-Fa-f0-9][A-Fa-f0-9])/) { [$1.hex].pack('C') } end end class Tempfile def self.open_with_block(name = nil, dir = nil) name ||= (0..8).map{rand(36).to_s(36)}.join args = dir ? [name, dir] : [name] tmp = Tempfile.open *args begin yield tmp ensure tmp.close true end end end class Dir def self.mkdir_r(dirPath) folders = dirPath.split('/') folderToCreate = '' folders.each do |folder| folderToCreate += folder + '/' if !File.directory?(folderToCreate) Dir.mkdir(folderToCreate) end end end end class Object # http://www.yohasebe.com/pages/trans-seeing-metaclasses-clearly/ def metaclass class << self self end end # http://www.yohasebe.com/pages/trans-seeing-metaclasses-clearly/ def meta_eval(&blk) metaclass.instance_eval(&blk) end # http://www.yohasebe.com/pages/trans-seeing-metaclasses-clearly/ def meta_def(name, &blk) meta_eval { define_method name, &blk } end # http://www.yohasebe.com/pages/trans-seeing-metaclasses-clearly/ def class_def(name, &blk) class_eval { define_method name, &blk } end # 1.8.7 エミュレート def tap yield(self) self end end