Skip to content

Instantly share code, notes, and snippets.

@arvind02
arvind02 / rails-jsonb-queries
Created August 25, 2022 19:47 — forked from mankind/rails-jsonb-queries
Ruby on Rails-5 postgresql-9.6 jsonb queries
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")
@arvind02
arvind02 / mac-setup-redis.md
Created September 7, 2021 12:56 — forked from tomysmile/mac-setup-redis.md
Brew install Redis on Mac

type below:

brew update
brew install redis

To have launchd start redis now and restart at login:

brew services start redis
@arvind02
arvind02 / active_record.rb
Created August 25, 2021 16:30 — forked from jackrg/active_record.rb
Add bulk import functionality to Rails Active Record (add this file to config/initializers, call <model>.import!(array-of-record-hashes))
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)
@arvind02
arvind02 / example1
Created July 24, 2020 05:56 — forked from daipresents/example1
Ruby rest-client file upload as multipart
require 'rest-client'
RestClient.get(url, headers={})
RestClient.post(url, payload, headers={})
@arvind02
arvind02 / s3_folder_upload.rb
Created December 12, 2019 06:14 — forked from fleveque/s3_folder_upload.rb
Upload folder to S3 recursively with ruby, multi threads and aws-sdk v2 gem, based on http://avi.io/blog/2013/12/03/upload-folder-to-s3-recursively/
#!/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
@arvind02
arvind02 / readme.md
Created November 5, 2019 05:44 — forked from maxivak/readme.md
Integrating Gem/Engine and Main Rails App
@arvind02
arvind02 / heap.rb
Created July 11, 2019 12:41 — forked from bogdanovich/heap.rb
Heap data structure in ruby
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
@arvind02
arvind02 / bfs_dfs.rb
Created June 27, 2019 12:37
BFS and DFS in Ruby
class Node
attr_accessor :value, :left, :right, :name
def initialize(options={})
@value = options[:value]
@name = options[:name]
end
def children
[@left, @right].compact
@arvind02
arvind02 / maximum_hourglass_sum.rb
Created April 5, 2019 07:10
ruby maximum hourglass sum
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|
@arvind02
arvind02 / img_url.rb
Created March 27, 2019 13:00
Simple ruby script to parse html file and download image resource. it stores all downloaded images into image directory of working directory
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