type below:
brew update
brew install redis
To have launchd start redis now and restart at login:
brew services start redis
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query | |
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails | |
#payload: [{"kind"=>"person"}] | |
Segment.where("payload @> ?", [{kind: "person"}].to_json) | |
#data: {"interest"=>["music", "movies", "programming"]} | |
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json) | |
Segment.where("data #>> '{interest, 1}' = 'movies' ") | |
Segment.where("jsonb_array_length(data->'interest') > 1") |
type below:
brew update
brew install redis
To have launchd start redis now and restart at login:
brew services start redis
class ActiveRecord::Base | |
def self.import!(record_list) | |
raise ArgumentError "record_list not an Array of Hashes" unless record_list.is_a?(Array) && record_list.all? {|rec| rec.is_a? Hash } | |
return record_list if record_list.empty? | |
(1..record_list.count).step(1000).each do |start| | |
key_list, value_list = convert_record_list(record_list[start-1..start+999]) | |
sql = "INSERT INTO #{self.table_name} (#{key_list.join(", ")}) VALUES #{value_list.map {|rec| "(#{rec.join(", ")})" }.join(" ,")}" | |
self.connection.insert_sql(sql) |
require 'rest-client' | |
RestClient.get(url, headers={}) | |
RestClient.post(url, payload, headers={}) |
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'aws-sdk' | |
class S3FolderUpload | |
attr_reader :folder_path, :total_files, :s3_bucket, :include_folder | |
attr_accessor :files | |
# Initialize the upload class |
class Heap | |
def initialize(type = :min) | |
@type = type | |
if @type == :min | |
@heap = [-1.0/0] | |
@compare = ->(a,b) { (a <=> b) < 0 } | |
elsif @type == :max | |
@heap = [1.0/0] | |
@compare = ->(a,b) { (b <=> a) > 0 } | |
else |
class Node | |
attr_accessor :value, :left, :right, :name | |
def initialize(options={}) | |
@value = options[:value] | |
@name = options[:name] | |
end | |
def children | |
[@left, @right].compact |
def sum_hour_glass(c, r, two_d_array) | |
top = two_d_array[r][c..c+2].inject(:+) | |
center = two_d_array[r +1][c+1] | |
bottom = two_d_array[r+2][c..c+2].inject(:+) | |
top + center + bottom | |
end | |
def array2D(arr) | |
max = -1.0/0.0 | |
arr.each_with_index do |row, r_i| |
require 'net/http' | |
require 'nokogiri' | |
# This script parse and download image files from html documents | |
def get_html(url) | |
uri = URI(url) | |
response = Net::HTTP.start(uri.host, uri.port, | |
:use_ssl => uri.scheme == 'https') do |http| | |
resp = http.get(uri.path) | |
case resp |