Skip to content

Instantly share code, notes, and snippets.

@TimLang
TimLang / gist:7635787
Created November 25, 2013 03:19
format xml in VIM
:!tidy -mi -xml %
@TimLang
TimLang / gist:7523727
Last active December 28, 2015 15:39
group array to range
values = [1,3,4,4,4,4,4,10,12,15,18]
bin_width = 5
ranges = 0.step((values.size/bin_width.to_f).ceil*bin_width, bin_width).each_cons(2).map { |s, e| Range.new(s, e, true) }
@TimLang
TimLang / si.rb
Created November 6, 2013 03:53 — forked from rasefon/si.rb
require 'RMagick'
$scale_size = 256.0
$img_fn1 = ARGV[0]
$img_fn2 = ARGV[1]
$scale_size = ARGV[2].to_f if ARGV[2]
def calculate_threshold(img_fn)
dir_name = File.dirname(img_fn)
@TimLang
TimLang / gist:7246194
Created October 31, 2013 08:41
Array to table format
class Array
def to_table_format
return self unless self.grep(/\${image\d+}/).empty?
cached_max = {}
unless self[0].class == Array
cached_max[0] = self.map(&:size).max
else
self[0].size.times.each do |i|
@TimLang
TimLang / gist:7150968
Created October 25, 2013 07:52
Group to hash by some rule
data = [{"weight"=>0, "id"=>8153, "text"=>"submits", "blank_id"=>"color1"}, {"weight"=>100, "id"=>3559, "text"=>"reveals", "blank_id"=>"color1"}, {"weight"=>0, "id"=>9458, "text"=>"launches", "blank_id"=>"color1"}, {"weight"=>0, "id"=>552, "text"=>"generates", "blank_id"=>"color1"}, {"weight"=>0, "id"=>2495, "text"=>"audience", "blank_id"=>"color2"}, {"weight"=>0, "id"=>8127, "text"=>"officials", "blank_id"=>"color2"}, {"weight"=>0, "id"=>8535, "text"=>"partners", "blank_id"=>"color2"}, {"weight"=>100, "id"=>6025, "text"=>"staff", "blank_id"=>"color2"}, {"weight"=>100, "id"=>2260, "text"=>"while", "blank_id"=>"color3"}, {"weight"=>0, "id"=>2920, "text"=>"because", "blank_id"=>"color3"}, {"weight"=>0, "id"=>4023, "text"=>"for", "blank_id"=>"color3"}, {"weight"=>0, "id"=>6816, "text"=>"although", "blank_id"=>"color3"}, {"weight"=>0, "id"=>113, "text"=>"exits", "blank_id"=>"color4"}, {"weight"=>100, "id"=>3528, "text"=>"shortages", "blank_id"=>"color4"}, {"weight"=>0, "id"=>1980, "text"=>"absences", "blank_id"=>
@TimLang
TimLang / gist:7130844
Created October 24, 2013 03:28
open-uri timeout
the simple way is
require 'timeout'
retries = 42
begin
Timeout::timeout(s){
open('http://a.url.com') do |f|
# ... stuff with f
@TimLang
TimLang / number_to_alph
Created October 22, 2013 02:49
Number to alph
class Numeric
Alph = ("a".."z").to_a
def alph
s, q = "", self
(q, r = (q - 1).divmod(26)) && s.prepend(Alph[r]) until q.zero?
s
end
end
3.alph
@TimLang
TimLang / gist:7001424
Created October 16, 2013 01:47
Checking your file encoding
brew install uchardet
$uchardet your file
#puts "UTF-8"
@TimLang
TimLang / encding_decode_image
Created October 12, 2013 09:03
encoding image to base64 str. decoding base64 str to image file.
#encoding image to base64
base64_str = Base64.encode64(File.open('Your_input_path'){|f| f.read})
#decoding base64 str to image file
File.open('your_output_path', 'w') {|f| f.write(Base64.decode64(base64_str))}
=begin
Message Pack vs similar utilities
ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.0.0], MBARI 0x6770, Ruby Enterprise Edition 2010.02
Packing
user system total real
pack: bert 18.320000 0.770000 19.090000 ( 19.101583)
pack: bson_ext 0.850000 0.030000 0.880000 ( 0.878398)
pack: json_ext 2.540000 0.040000 2.580000 ( 2.582495)