Skip to content

Instantly share code, notes, and snippets.

View cjbottaro's full-sized avatar

Christopher J. Bottaro cjbottaro

  • AcademicWorks, Inc
  • Austin, Tx
View GitHub Profile
@cjbottaro
cjbottaro / gist:9400140
Last active August 29, 2015 13:57
Promises and event handlers ?
doShow = ->
new RSVP.Promise (resolve, reject) ->
$("blah").off("shown.bs.modal")
$("blah").on "shown.bs.modal", (e) ->
resolve("I'm shown!")
$("blah").modal("show")
doShow().then (message) ->
console.log(message) # => "I'm shown!"
@cjbottaro
cjbottaro / gist:10920007
Last active August 29, 2015 13:59
duplicate percolator results
require "elasticsearch"
client = Elasticsearch::Client.new
if !client.indices.exists(index: "percolator_test")
client.indices.create index: "percolator_test"
end
client.indices.put_mapping index: "percolator_test",
type: "type1",
@cjbottaro
cjbottaro / gist:2bfd0e8582d9ee2e083c
Created August 12, 2014 17:15
Keep track of parent thread when spawning new thread
class Thread
def self.spawn(*args, &block)
new(Thread.current, *args) do |*args|
Thread.current[:parent] = args.first
block.call(*args[1..-1])
end
end
singleton_class.class_eval{ private :new }
@cjbottaro
cjbottaro / gist:19b5919cca4cdbec5830
Last active August 29, 2015 14:14
Anagram detector in Ruby
def anagram?(s1, s2)
s1.downcase.chars.sort == s2.downcase.chars.sort
end
# General structure of a recursive function
def f
if condition
# Base case. Stops the loop
else
# Recursive case. Continues loop.
end
end
# Addition implemented with normal iteration.
@cjbottaro
cjbottaro / gist:cc5d7d3ae4329e35d7f9
Created April 27, 2015 20:34
log_lock_waits output (partial)
[scholarship_app_development] STATEMENT: UPDATE applications SET updated_at = '2015-04-27 20:25:35 +0000' WHERE id = 455423
[scholarship_app_development] LOG: process 97776 still waiting for ExclusiveLock on tuple (408,65) of relation 5436886 of database 5158717 after 1000.090 ms
[scholarship_app_development] STATEMENT: UPDATE applications SET updated_at = '2015-04-27 20:25:29 +0000' WHERE id = 455423
[scholarship_app_development] LOG: process 96629 still waiting for ExclusiveLock on tuple (408,65) of relation 5436886 of database 5158717 after 1000.175 ms
[scholarship_app_development] STATEMENT: UPDATE applications SET updated_at = '2015-04-27 20:25:22 +0000' WHERE id = 455423
[scholarship_app_development] LOG: process 97110 still waiting for ExclusiveLock on tuple (408,65) of relation 5436886 of database 5158717 after 1001.027 ms
[scholarship_app_development] STATEMENT: UPDATE applications SET updated_at = '2015-04-27 20:25:32 +0000' WHERE id = 455423
[scholarship_app_development] LOG: process 96891
@cjbottaro
cjbottaro / gist:a966008af75fbbd874e1
Created June 8, 2015 21:04
Chef bootstrap error
Connecting to 10.1.13.242
10.1.13.242 Installing Chef Client...
10.1.13.242 --2015-06-08 20:51:43-- https://www.opscode.com/chef/install.sh
10.1.13.242 Resolving www.opscode.com (www.opscode.com)... 184.106.28.91
10.1.13.242 Connecting to www.opscode.com (www.opscode.com)|184.106.28.91|:443... connected.
10.1.13.242 HTTP request sent, awaiting response... 504 Gateway Time-out
10.1.13.242 2015-06-08 20:53:44 ERROR 504: Gateway Time-out.
10.1.13.242
10.1.13.242 Starting first Chef Client run...
10.1.13.242 bash: line 88: chef-client: command not found
@cjbottaro
cjbottaro / gist:6f8f8f2f235a2293fe62
Last active August 29, 2015 14:26
Installing MUQ on OS X
brew install boost --c++11
brew tap homebrew/science
brew install eigen flann hdf5 sundials gcc
cd /tmp
git clone https://bitbucket.org/mituq/muq
cd muq/MUQ/build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/muq ..
make && make install
class Person < ActiveRecord::Base
has_many :addresses
validates_associated :addresses
validates_length_of :addresses, :less_than => 5
before_save :save_addresses
def addresses_from_form=(address_hashes)
addresses = address_hashes.collect{ |address_hash| Address.new(address_hash) }
self.addresses.target = addresses # sets the association target, doesn't trigger any saves or destroys
# this works because validates_associated basically just does self.addresses.each{ |address| address.valid? }
require 'rubygems'
require 'activesupport'
dt = DateTime.now
puts dt.to_f
# => 1240455468.91318
t = YAML.load(dt.to_yaml)
puts t.class.name
# => Time
puts t.to_f
# => 1240455468.0