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
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
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
# 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
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
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
@raypereda
raypereda / word_count.go
Created April 2, 2014 04:08
count the words in a string
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
"fmt"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
@raypereda
raypereda / array_practice.go
Created April 2, 2014 04:35
practice using arrays in go
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for i, _ := range pic {
pic[i] = make([]uint8, dx)
for j := 0; j < dy; j++ {
pic[i][j] = uint8(i*j)
@raypereda
raypereda / fibo.go
Created April 2, 2014 05:01
fibonacci in go
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func(int) int {
return func(n int) int {
a, b := 0, 1
for i := 0; i < n; i++ {
package main
import "fmt"
func Cbrt(x complex128) complex128 {
z := x
for i := 0; i < 10; i++ {
z = z - (z*z*z - x)/ (3*z*z)
}
return z
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %g ", float64(e))