Skip to content

Instantly share code, notes, and snippets.

@AlexWayfer
AlexWayfer / alias_task.rake
Created June 23, 2016 13:22 — forked from raggi/alias_task.rake
A helper to alias a task in rake
def alias_task(name, old_name)
t = Rake::Task[old_name]
desc t.full_comment if t.full_comment
task name, *t.arg_names do |_, args|
# values_at is broken on Rake::TaskArguments
args = t.arg_names.map { |a| args[a] }
t.invoke(*args)
end
end
@AlexWayfer
AlexWayfer / including.rb
Last active November 7, 2016 21:50
Example for meta including
# Including neccessary modules from ./base/modules
Dir.glob(File.join 'base', 'modules', '*.rb').each do |mod|
require mod
basename = File.basename mod, '.*'
include const_get basename.split('_').map(&:capitalize).join
# With helpers:
# include basename.camelize.constantize
end
@AlexWayfer
AlexWayfer / is_empty.rb
Created November 9, 2016 07:17
Checking of object empty
class MyObject
attr_accessor :foo, :bar
def initialize(foo = nil, bar = nil)
self.foo = foo
self.bar = bar
end
def empty?
foo.nil? || foo.empty?
@AlexWayfer
AlexWayfer / fibonacci.rb
Created May 1, 2017 22:06
MRI Multi-threading
# frozen_string_literal: true
def fibonacci(n)
return n if n <= 1
fibonacci(n - 1) + fibonacci(n - 2)
end
def calculate(id: 1, n: 40)
start = Time.now
puts "Start ##{id}: #{start}"
@AlexWayfer
AlexWayfer / output
Created September 14, 2018 02:15
Ruby Transducers benchmark
> ruby test.rb
I, [2018-09-14T05:14:28.929558 #30046] INFO -- : true
Warming up --------------------------------------
native 1.000 i/100ms
baseline 2.000 i/100ms
lazy 1.000 i/100ms
via_transduce 1.000 i/100ms
Calculating -------------------------------------
native 11.293 (± 0.0%) i/s - 57.000 in 5.054247s
baseline 21.711 (± 4.6%) i/s - 110.000 in 5.077083s
# frozen_string_literal: true
def db_migrations
return @db_migrations if defined?(@db_migrations)
puts "Request to DB from #{self.class} (#{object_id})"
@db_migrations = [3, 5]
end
@AlexWayfer
AlexWayfer / results.md
Created February 12, 2020 11:36
Const check performance
# frozen_string_literal: true

# require 'pry-byebug'

require 'benchmark'
require 'benchmark/ips'
require 'benchmark/memory'
@AlexWayfer
AlexWayfer / benchmark.rb
Last active August 20, 2020 17:34
Benchmark example
# frozen_string_literal: true
require 'bundler/setup'
Bundler.setup :system
require 'pry-byebug'
require 'benchmark'
require 'benchmark/ips'
require 'benchmark/memory'
@AlexWayfer
AlexWayfer / benchmark.md
Created June 26, 2020 09:08
Benchmark between `Array#|` and `#uniq`
# frozen_string_literal: true

require 'bundler/setup'
Bundler.setup :system

require 'pry-byebug'

require 'benchmark'
require 'benchmark/ips'