Skip to content

Instantly share code, notes, and snippets.

View rhearnorth's full-sized avatar

Ronnakit vijidboonchuvong rhearnorth

View GitHub Profile
require 'benchmark'
[1, nil, 'string', :sym, Class].each do |sample|
Benchmark.bmbm do |x|
x.report("case .class.name") do
100.times do
case sample.class.name
when 'String' then 'String'
when 'Symbol' then 'Symbol'
when 'NilClass' then 'NilClass'
@rhearnorth
rhearnorth / any_all_none_benchmark.rb
Last active August 29, 2015 14:17
Benchmarking any? all? none?
require 'benchmark'
cases = [
[true, true, true, true, false, true, true, true, true, false], # Mix
[true, true, true, true, true, true, true, true, true, true], # All true
[false, false, false, false, false, false, false, false, false, false] # All false
]
Benchmark.bmbm do |x|
cases.each do |test_case|
x.report("any?") do
@rhearnorth
rhearnorth / Benchmark underscorize hash
Last active August 29, 2015 14:14
Underscorize Hash
require 'benchmark'
Benchmark.bmbm do |x|
HASH_TEST = {'a' => 'a', 'b' => { 'c' => {'firstName' => 'd'}, 'e' => 'e'}}
def convert_hash_keys(value)
case value
when Array
value.map { |v| convert_hash_keys(v) }
when Hash
Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
@rhearnorth
rhearnorth / merge_products.rb
Last active August 29, 2015 14:10
Script for merge product with the same name. (Move variants and delete other products)
module MergeProducts
# Example
# => MergeProducts.merge_into(Product.first)
def self.merge_into(product)
fail "This is 'achived' product" if product.status.eql?('archived')
"Starting merge products ... "
# Search all product with exact same name
other_products = product.account.products.where(name: product.name).where.not(id: product.id, status: 'archived')
other_variants = other_products.collect(&:variants).flatten