Skip to content

Instantly share code, notes, and snippets.

source :rubygems
gem 'rugged', '0.17.0.b6'
@sandinist
sandinist / gist:3221827
Created July 31, 2012 23:56
codequiz.rb
require 'net/http'
require 'json'
response = Net::HTTP.get_response("codejp2012quiz.cloudapp.net","/Quiz/GetList")
quiz = JSON::parse response.body
p quiz
require 'mathn'
MAX = 30
AMAX = 10
BMAX = 20
class Pages
attr_accessor :name, :line, :line_of_page
def initialize; yield self; end
def last; (@line / @line_of_page).ceil; end
def line_at_last; (mod_line == 0) ? @line_of_page : mod_line ; end
require 'mathn'
MAX = 30
AMAX = 10
BMAX = 20
def conditions(page, count_a, count_b)
max_page_a = (count_a / AMAX).ceil
max_page_b = (count_b / BMAX).ceil
return nil if page > max_page_a && page > max_page_b
return {offset_a: (page - 1) * AMAX, limit_a: AMAX, offset_b: (page - 1) * BMAX, limit_b: BMAX} if page < max_page_a && page < max_page_b
MAX = 30
AMAX = 10.0
BMAX = 20.0
def limit_page(count_a, count_b)
ap = (count_a / AMAX).ceil
bp = (count_b / BMAX).ceil
return ap, bp if ap == bp
if ap < bp
@sandinist
sandinist / gist:2867234
Created June 4, 2012 08:27
Format Time object from year to milliseconds. (more easily?)
Time.now.instance_eval {'%s.%03d' % [strftime('%Y/%m/%d %H:%M:%S'), (usec / 1000)]}
@sandinist
sandinist / gist:2849769
Created June 1, 2012 07:06
.gitconfig for tig
[tig "bind"]
generic = g move-first-line
generic = G move-last-line
generic = f move-page-down
generic = b move-page-up
@sandinist
sandinist / gist:2835165
Created May 30, 2012 09:44
truncate tables
tables = (ActiveRecord::Base.connection.tables - %w{schema_migrations})
tables.each {|table|
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table};")
}
@sandinist
sandinist / gist:2427434
Created April 20, 2012 09:56
rake info:count
namespace :info do
desc "Count record each tables"
task(count: :environment) {
tables = ActiveRecord::Base.connection.tables
tables.reject!(&"schema_migrations".method(:==))
tables.map!(&:classify)
len = tables.map(&:length).max
tables.each{|table|
puts "#{table.ljust(len)}: #{Object.const_get(table).count}"
}
@sandinist
sandinist / gist:2419008
Created April 19, 2012 06:02
Change the key to Hashes in the Array from string to symbol.
src = [{"a"=>"aaa", "b"=>"bbb"}, {"c"=>"ccc"}]
src.map{|x| x.each_with_object({}) {|(k, v), h| h[k.to_sym] = v}}
#=> [{:a=>"aaa", :b=>"bbb"}, {:c=>"ccc"}]