Skip to content

Instantly share code, notes, and snippets.

View goshacmd's full-sized avatar

Gosha Spark goshacmd

View GitHub Profile
@goshacmd
goshacmd / demo.rb
Created December 28, 2012 17:12
Injecting method arguments
i = Injector.new first: 'First Name', last: 'Last Name', power: 2
def full_name(first, last)
"#{first} #{last}"
end
def random_to_power(power)
4 ** power
end
@goshacmd
goshacmd / rake.log
Created December 29, 2012 10:05
active_admin specs failing on 1.9.3-p362, OS X ML
This file has been truncated, but you can view the full file.
4 processes for 77 specs, ~ 19 specs per process
.....DEPRECATION WARNING: Active Admin: ActiveAdmin.default_namespace is deprecated. Please use ActiveAdmin.application.default_namespace. (called from block (3 levels) in <top (required)> at /Users/goshakkk/Projects/active_admin/spec/unit/active_admin_spec.rb:8)
.............................................................................Running JSLint:
......................*****
No JS errors found.
..............................DEPRECATION WARNING: ActiveAdmin::Dashboard is deprecated and will be removed in the next version
...........................................................................................................................F*.....................................................*.........................................................................................*..*.......*............................................................*.........
require 'benchmark'
# Benchmarking Array#compact & Array#join
n = 1_000_000
Benchmark.bm do |x|
x.report('3 elements + nil') { n.times { ['a', 'b', 'c', nil].compact.join('::') } }
x.report('3 elements') { n.times { ['a', 'b', 'c'].compact.join('::') } }
end
# user system total real
@goshacmd
goshacmd / bm.rb
Last active December 10, 2015 12:38
Array() vs #flatten
require 'benchmark'
n = 1_000_000
Benchmark.bm do |x|
x.report('[[1]]') { n.times { [[1]].flatten } }
x.report('Array([1])') { n.times { Array([1]) } }
x.report('[1]') { n.times { [1].flatten } }
x.report('Array(1)') { n.times { Array(1) } }
end
@goshacmd
goshacmd / README.md
Created January 7, 2013 17:05
Rails-generated app README idea

AppName

Insert brief description of your app.

Requirements

  • Ruby 1.9.3+
  • Bundler 1.3.0
  • PostgreSQL
@goshacmd
goshacmd / inline_relations.rb
Created January 16, 2013 19:58
Mongoid inline relation definitions. Ruby makes it so easy ❤️
# Mongoid inline relation definitions. Ruby makes it so easy.
# (`data` is effectively just a shortcut for `inline_embed_one(:data)` there.)
#
# Yes, it would make sense to create separate models, but my case is different.
# This application is used to track events of other apps, such as purchase, and
# it makes *more* sense to embed all order-related data. Instead of having model
# overload for each event being tracked, I just define them inline.
class OrderEvent
include Analyzing::Event
@goshacmd
goshacmd / bm.txt
Created January 20, 2013 20:01
MongoDB: Map/Reduce vs Aggregation framework on 1M docs. Basically just counting how many times was made each request (only 250K of requests had search queries). Aggregation turned to be 84x faster.
1000000 documents
249819 documents with non-empty 'search_query'
user system total real
simple map/reduce 0.270000 0.010000 0.280000 (353.701946)
simple aggregation 0.180000 0.010000 0.190000 ( 8.049170)
filtering map/reduce 0.250000 0.010000 0.260000 (337.955130)
filtering aggregation 0.150000 0.010000 0.160000 ( 4.095468)
$ cat bm.rb
require './env'
Benchmark.bm do |x|
x.report('block') { OrderEvent.all.limit(100_000).map(&:total) }
x.report('sym') { OrderEvent.all.limit(100_000).map(:total) }
end
$ ruby bm.rb
user system total real
@goshacmd
goshacmd / fix_sys_rb.sh
Created June 12, 2013 07:41
OS X [REDACTED] Ruby 2.0 headers fix.
sys_rb_usr=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr
sdk_rb_usr=`xcode-select -p`/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr
sudo cp -r $sdk_rb_usr/include $sys_rb_usr/include