Skip to content

Instantly share code, notes, and snippets.

View sgonyea's full-sized avatar

Scott Gonyea sgonyea

  • Google
  • Mountain View, CA
View GitHub Profile
@sgonyea
sgonyea / ruby_asterisk_operator.rb
Created June 28, 2011 06:13
Ruby's Asterisk Operator is so useful
attrs = [*params['records']].pop || {}
@note = Note.find(params[:id])
attrs.delete :location_id
@note.update_attributes(attrs.slice(*Note.attribute_names))
@sgonyea
sgonyea / Rakefile
Created June 12, 2011 01:11
How to get jasmine to work with CoffeeScript
begin
require 'bundler'
rescue => e
puts 'Bundler could not be loaded. Please run `gem install bundler` from the command-line, and then `bundle install`.'
raise e
end
Bundler.require :default
load 'jasmine/tasks/jasmine.rake'
require 'active_support/core_ext/array/extract_options'
# takes a number and options hash and outputs a string in any currency format
# @param [Numeric, #to_f] number The float-able value, to be currencified
# @see Numeric#currencify
def Currencify(number, *opts)
number.to_f.currencify(number, *opts)
end
class Numeric
require 'strscan'
module StringStubber
WORD = /\w+[\!\.\?]*[^\s,]/
def scanner
@scanner ||= StringScanner.new(self)
end
# Scans ahead one word position, and returns it
@sgonyea
sgonyea / rake_specs
Created March 2, 2011 07:57
github.com/sgonyea/string_stubber
sgonyea@skg:~/Sites/string_stubber$ rvm exec "ruby -v && rake spec"
rubinius 1.2.3dev (1.8.7 2c952b20 yyyy-mm-dd JI) [x86_64-apple-darwin10.6.0]
(in /Users/sgonyea/Sites/string_stubber)
/Users/sgonyea/.rvm/rubies/rbx-head/bin/rbx -S bundle exec rspec --color spec/quality_spec.rb spec/string_stubber/base_spec.rb spec/string_stubber/core_ext_spec.rb spec/string_stubber/readme_spec.rb
The application itself
has no malformed whitespace
Stubber::Base
The Base module should behave like a proper mix-in ;)
sgonyea@skg:~/Sites/spidr$ rake spec --trace
(in /Users/sgonyea/Sites/spidr)
** Invoke spec (first_time)
** Execute spec
/Users/sgonyea/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -S bundle exec rspec ./spec/actions_spec.rb ./spec/agent_spec.rb ./spec/auth_store_spec.rb ./spec/cookie_jar_spec.rb ./spec/extensions/uri_spec.rb ./spec/filters_spec.rb ./spec/page_spec.rb ./spec/rules_spec.rb ./spec/sanitizers_spec.rb ./spec/spidr_spec.rb
Spidr::Actions
should be able to pause spidering
should be able to continue spidering after being paused
should allow skipping of enqueued links
require 'strscan'
start = "KEYWORD_START"
end = "KEYWORD_END"
regex = Regexp.new "#{start}(.+?)#{end}", false
text = 'Lorem ipsum dolor sit amet, KEYWORD_STARTconsecteturKEYWORD_END adipiscing elit. Praesent tellus nisl, KEYWORD_STARTconsequatKEYWORD_END at auctor ut, consectetur eget diam. Mauris eget magna ac nunc cursus dapibus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla magna orci, faucibus non aliquam id, fermentum non nisi. Cras ac tortor a lacus vestibulum aliquam ut rutrum mauris. KEYWORD_STARTIntegerKEYWORD_END rutrum ultrices euismod. KEYWORD_STARTPellentesque quis libero ligulaKEYWORD_END, eu elementum enim. Quisque sed luctus dui. Suspendisse vel enim eget lorem cursus euismod. Aliquam condimentum ultricies tincidunt. Nam leo urna, placerat vel vulputate sed, condimentum id lacus. Proin imperdiet, KEYWORD_STARTloremKEYWORD_END quis mattis elementum, ligula justo volutpat augue, id blandit ligula neque eu ante. Etiam massa justo, iaculis n
@sgonyea
sgonyea / 01-nice_enumeration.rb
Created December 22, 2010 00:03
I love Enumerable
# @param [true, false] qt Whether or not Keywords are surrounded by quotes
# @return [Array] The list of all Keyword-words (with quotes if specified)
def self.all_words(qt=false)
words = select(:word).map(&:word)
qt && words.map(&:inspect) || words
end
1) Member#destroy keeps the record around by setting a 'deleted_at' flag
Failure/Error: Member.only_deleted.first.deleted_at.should be
PGError: ERROR: invalid input syntax for type timestamp: "t"
LINE 2: OR members.deleted_at != 't') LIMIT 1
^
: SELECT "members".* FROM "members" WHERE (members.deleted_at IS NOT NULL
OR members.deleted_at != 't') LIMIT 1
# ./spec/models/member_spec.rb:116:in `block (3 levels) in <top (required)>'
['excon', 'json'].each{|lib| require lib}
award = (ARGV[0] || (print "Please Enter Bet Ammount: "; STDIN.gets)).to_i * 35
award = 0 unless "13" == number = JSON.parse(Excon.get("http://roulette.engineyard.com/").body)["winning_number"]
puts "OMG WOW (#{number}), YOU SHOULD PLAY AGAIN! You won: #{award}"