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 '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' |
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 '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 |
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 '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)] }] |
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
| 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 |