Skip to content

Instantly share code, notes, and snippets.

View raypereda's full-sized avatar

Ray Pereda raypereda

  • Genesis Research Group
  • Long Beach, CA
View GitHub Profile
def diff(one, other)
(one.keys + other.keys).uniq.inject({}) do |memo, key|
unless one.key?(key) && other.key?(key) && one[key] == other[key]
memo[key] = [one.key?(key) ? one[key] : :_no_key, other.key?(key) ? other[key] : :_no_key]
end
memo
end
end
require 'mongo'

primary = Mongo::MongoClient.from_uri('mongodb://nbuild:<password>@mongo3.nbuild.prd.atl.3dna.io/nbuild')
primary_collection = primary['nbuild']['oauth_access_tokens']
primary_collection.count
 # => 203
slave = Mongo::MongoClient.from_uri('mongodb://nbuild:<password>@mongo-delayed.nbuild.prd.atl.3dna.io/nbuild?slaveok=true')

slave_collection = slave['nbuild']['oauth_access_tokens']
slave_collection.count
 # => 128481
# A few examples about how to use Ruby for parsing files as we could do
# with Awk or Grep. This is based on what I learn fro this post:
# http://code.joejag.com/2009/using-ruby-as-an-awk-replacement/
# Split each line with ':' and print the first $F[0] field
awk -F: '{ print $1 }' /etc/passwd
ruby -F: -nae 'puts $F[0]' /etc/passwd
# Parse the 'ps aux' output
# It'll print the ID process for the 'jojeda' user
def self.process_results(num)
client.process_results(num) do |address_hash| # Redis LPOP and a JSON parse should be fast
Nation.with(address_hash['passthrough']['nation_slug']) do # Is Nation.with slow???
address = Address.find_by_id(address_hash['passthrough']['address_id']) # find_by_id is should be fast
next unless address
begin
address.update_geocode_data(address_hash) # Is update_geocode_data slow?
rescue ActiveRecord::RecordInvalid
require 'charlock_holmes'
def is_utf8?(data)
data.force_encoding('UTF-8').valid_encoding?
end
def detect_with_charlock_holmes(data)
detection = CharlockHolmes::EncodingDetector.detect(data)
return detection[:encoding]
end
{
"auto_complete": false,
"color_scheme": "Packages/Theme - Flatland/Flatland Monokai.tmTheme",
"font_size": 16,
"ignored_packages":
[
],
"ruby_eval":
{
"ruby": "/Users/nbdev/.rvm/rubies/ruby-1.9.3-p327/bin/ruby"
[
// bind 'jj' to exit insert mode
{ "keys": ["j", "j"], "command": "exit_insert_mode",
"context":
[
{ "key": "setting.command_mode", "operand": false },
{ "key": "setting.is_widget", "operand": false }
]
},
@raypereda
raypereda / compile_doc.rb
Created August 27, 2013 23:43
convert from GitHub Markdown to HTML
#!/usr/bin/env ruby
require 'nokogiri'
require 'github/markup'
def preformatted_html
file = ARGV.first
GitHub::Markup.render(file, File.read(file))
end
#!/bin/bash
# nuke-dbs -- completely restore nbuild databases
cd $NBUILD
# Note: This script assumes that your checkout of nbuild is
# in ~/Workspace/nbuild.
# Exit if errors are encountered
@raypereda
raypereda / gist:5217456
Created March 21, 2013 22:36
ruby one-liner for running sum of floats, one per line
cat temp.txt | ruby -ne 's ||= 0.0; s += $_.to_f; puts s'