This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'mechanize' | |
FILE = 'crawler.log' | |
a = Mechanize.new { |agent| | |
agent.user_agent_alias = 'Mac Safari' | |
} | |
a.get('http://www.com/') do |page| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Invoice | |
attr_accessor :product_name | |
def email_invoice | |
puts "Emailing an invoice..." | |
puts name | |
end | |
def short_name | |
puts name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Invoice | |
attr_accessor :product_name | |
# default name | |
def name | |
"Invoice for #{@product_name}" | |
end | |
end | |
class InvoiceEmail < Invoice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrderRepor | |
attr_accessor :attributes | |
def initialize(various_attributes) | |
@attributes = various_attributes | |
end | |
def print_out | |
puts "Summary title" | |
puts @attributes[:item_name] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrderRepor | |
attr_accessor :attributes | |
def initialize(various_attributes) | |
@attributes = various_attributes | |
end | |
def print_out | |
puts "Summary title" | |
puts @attributes[:item_name] |
OlderNewer