Skip to content

Instantly share code, notes, and snippets.

@jamescway
jamescway / test.rb
Created April 2, 2011 22:44
test script to test selenium webdriver
********************************************************************************
Using script:
require 'rubygems'
require 'selenium-webdriver'
$DEBUG = true
Selenium::WebDriver.for(:firefox).quit
Results below:
ActiveRecord::Schema.define(:version => 20110613211706) do
create_table "answers", :force => true do |t|
t.text "answer"
t.integer "question_id"
end
create_table "questions", :force => true do |t|
t.string "question"
end
%a.btn-sec.offer-button{:href => "#", :class => [offers_enabled_class, (is_linked && "linked" || "unlinked")], 'attr-deal_id' => deal.id, 'attr-logged_in' => "#{logged_in?}", 'attr-title' => deal.title, 'attr-merchant' => deal.merchant.name, 'attr-expires' => deal.end_at.strftime('%b %-d, %Y'), 'attr-image_url' => ugasset_url(deal.image)}
= (is_linked ? 'Unlink from Card' : 'Link to Card')
@jamescway
jamescway / gist:3682043
Created September 9, 2012 01:56
bloomfilter
require 'digest'
require 'murmurhash3' #gem install murmurhash3
require 'trollop'
class BloomFilter
def initialize(size)
@size = size.to_i || 2_000_000
@num_inputs = 0
@bloom_filter = Array.new(size)
@jamescway
jamescway / gist:3720779
Created September 14, 2012 08:32
Anagram
require File.expand_path(File.join(File.dirname(__FILE__), "bloom.rb"))
class String
def each_char_with_index
0.upto(size - 1) do |index|
yield(self[index..index], index)
end
end
def remove_char_at(index)
return self[1..-1] if index == 0
require 'ruby-debug'
def multi_split(str, tokens)
results = []
buffer = ""
maybe_token = false
temp_str = ""
str.each_char do |c|
if maybe_token
if is_token?(temp_str + c, tokens)
@jamescway
jamescway / multi-select2.rb
Last active December 14, 2015 10:29
multi-select 2 recursive
def multi_split2(str, delims)
multi(str, delims, [])
end
def multi(str, delims, sum)
return sum if str.empty?
temp, token = "", ""
while !is_token_prefix? str[0], delims
token << str[0]
@jamescway
jamescway / multi-select-recursive-refactor.rb
Last active December 14, 2015 10:39
multi-select-recursive-refactor.rb
def is_token_prefix?(prefix, tokens)
tokens.each { |token| return true if token.start_with?(prefix) }
false
end
def is_token?(str, tokens)
tokens.each { |token| return true if token == str }
false
end
require 'ruby-debug'
class LineCounter
def initialize
@count = 0
@commenting = false
end
def lines_of_code(file)
@count = 0
@jamescway
jamescway / gist:5770667
Last active December 18, 2015 10:48
Find in nested hashes, prints key path recursively
def search_in_hash(hash, target, args={}, keys='ROOT')
find_key = (args[:find_key] == true)
hash.each do |key, value|
find_target = find_key ? key.to_s : value
if find_target == target
keys = "#{keys} -> '#{key}' => #{target}"
puts keys
keys = ""
end