Skip to content

Instantly share code, notes, and snippets.

View mleszcz's full-sized avatar

Mateusz Leszczyński mleszcz

View GitHub Profile
require 'rubygems'
require 'mechanize'
FILE = 'crawler.log'
a = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
a.get('http://www.com/') do |page|
@mleszcz
mleszcz / gist:1035523
Created June 20, 2011 12:20
count number of set bits in string representing hexadecimal number
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
@mleszcz
mleszcz / gist:1035594
Created June 20, 2011 13:23
complementary pairs of array items
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
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
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# Activate the gem you are reporting the issue against.
@mleszcz
mleszcz / solid.rb
Last active May 9, 2018 10:46
SOLID'nie
# In an object instance variable (denoted with '@'), remember a block.
def remember(&a_block)
@block = a_block
end
# Invoke the preceding method, giving it a block that takes a name.
remember {|name| puts "Hello, #{name}!"}
# Call the closure (note that this happens not to close over any free variables):
@block.call('Jon') # => "Hello, Jon!"
class Invoice
attr_accessor :product_name
def email_invoice
puts "Emailing an invoice..."
puts name
end
def short_name
puts name
class Invoice
attr_accessor :product_name
# default name
def name
"Invoice for #{@product_name}"
end
end
class InvoiceEmail < Invoice
class OrderRepor
attr_accessor :attributes
def initialize(various_attributes)
@attributes = various_attributes
end
def print_out
puts "Summary title"
puts @attributes[:item_name]
class OrderRepor
attr_accessor :attributes
def initialize(various_attributes)
@attributes = various_attributes
end
def print_out
puts "Summary title"
puts @attributes[:item_name]