This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"code.google.com/p/go-tour/wc" | |
"strings" | |
"fmt" | |
) | |
func WordCount(s string) map[string]int { | |
words := strings.Fields(s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++ { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type ErrNegativeSqrt float64 | |
func (e ErrNegativeSqrt) Error() string { | |
return fmt.Sprintf("cannot Sqrt negative number: %g ", float64(e)) |