View gist:1081491
1 2 3 4 5 6 7 8 9 10
module OrderedModel
def up
self.class.update_all 'ord = ord + 1', "ord = #{self.ord-1}"
update_attribute :ord, self.ord-1
end
 
def down
self.class.update_all 'ord = ord - 1', "ord = #{self.ord+1}"
update_attribute :ord, self.ord+1
end
View gist:1035594
1 2 3 4 5 6 7 8 9 10
def dups(arr, i, j)
l_dups, r_dups = 0, 0
i.upto(j) do |n|
break if arr[n] != arr[i]
l_dups +=1
end
j.downto(i) do |n|
break if arr[n] != arr[j]
r_dups += 1
end
View gist:1035523
1 2 3 4 5 6 7 8
def setbits_count(s)
# bits map
bm = {"0" => 0, "1" => 1, "2" => 1, "3" => 2, "4" => 1, "5" => 2, "6" => 2, "7" => 3,
"8" => 1, "9" => 2, "A" => 2, "B" => 3, "C" => 2, "D" => 3, "E" => 3, "F" => 4}
sum = 0
0.upto(s.size-1) { |i| sum += bm[s[i]] }
sum
end
View crawler.rb
1 2 3 4 5 6 7 8 9 10
require 'rubygems'
require 'mechanize'
 
FILE = 'crawler.log'
 
a = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
 
a.get('http://www.com/') do |page|
Something went wrong with that request. Please try again.