Skip to content

Instantly share code, notes, and snippets.

View vadviktor's full-sized avatar
๐Ÿ€
Mining Ruby ๐Ÿ’Ž โ›๏ธ

Viktor (Ikon) VAD ๐Ÿ€ vadviktor

๐Ÿ€
Mining Ruby ๐Ÿ’Ž โ›๏ธ
View GitHub Profile
@vadviktor
vadviktor / ip.sh
Created April 21, 2015 12:33
Get the host's docker bridge interface's ip address
/sbin/ip route | grep 'docker' | grep -E '([\.0-9]{8,15}[^0]$)' -o
@vadviktor
vadviktor / Gemfile
Created May 2, 2015 22:33
Process the btsynced photos and videos from our phones
source 'https://rubygems.org'
gem 'mini_magick'
gem 'activesupport'
@vadviktor
vadviktor / explain.rb
Created May 29, 2015 15:29
Educating myself in regular expressions
"
^ # Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed)
\\d # Match a single character that is a โ€œdigitโ€ (ASCII 0โ€“9 only)
{4} # Exactly 4 times
(?: # Match the regular expression below
# Match this alternative (attempting the next alternative only if this one fails)
# Match the regular expression below
(?: # Match the regular expression below
@vadviktor
vadviktor / fork.rb
Last active October 13, 2015 13:28
druby within the same script file (raw material, even missing mutexes, etc)
require 'drb/drb'
main_pid = Process.pid
f_proc_id = fork do
puts "Started server fork with pid: #{Process.pid}, gpid: #{Process.getpgid(main_pid)}"
class Front
def initialize
@global = [1,2,3,4]
@vadviktor
vadviktor / digg.rb
Created July 8, 2015 16:07
Do you have a deeply nested structures you want 1 value from deep inside? Donโ€™t care if the path is broken? Use digg! I especially love this to get deeply nested data out of a JSON response.
# origin: http://thingsinabucket.com/2015/07/01/three_little_hacks/
module Digg
def digg(*path)
path.inject(self) do |memo, key|
(memo.respond_to?(:[]) && (memo[key] || {})) || break
end
end
end
@vadviktor
vadviktor / find_arr_vs_hash.rb
Created July 14, 2015 14:13
Benchmark for finding item in Array and in Hash using #include? method
require 'benchmark'
a = ('a'..'zzzzz').to_a
h = {}
a.each {|e| h[e] = :ok}
Benchmark.bmbm do |x|
x.report("find z in array") { a.include? 'z' }
x.report("find zz in array") { a.include? 'zz' }
x.report("find zzz in array") { a.include? 'zzz' }
@vadviktor
vadviktor / wait _for_all_children.rb
Created July 18, 2015 10:57
Wait for all of the forked child processes to finish
pids = []
(1..(ENV['CPUCORES'] || 3)).each do
pids << fork do
# do your stuff
end
end
# hold your breath until all children finish
@vadviktor
vadviktor / fork_with_new_connection.rb
Last active August 29, 2015 14:25 — forked from danieldbower/fork_with_new_connection.rb
Forking Processes In Ruby On Rails
# Logic for forking connections
# The forked process does not have access to static vars as far as I can discern, so I've done some stuff to check if the op threw an exception.
def fork_with_new_connection
# Store the ActiveRecord connection information
config = ActiveRecord::Base.remove_connection
pid = fork do
# tracking if the op failed for the Process exit
success = true
@vadviktor
vadviktor / db.rb
Created September 4, 2015 09:39
Rails connection pool management with test
def safely
begin
unless ActiveRecord::Base.connected?
ActiveRecord::Base.connection.verify!(0)
end
yield
rescue => e
status e.inspect
ensure
ActiveRecord::Base.connection_pool.release_connection
@vadviktor
vadviktor / benchmark.rb
Created September 26, 2015 20:14
Ruby empty? vs any?
require 'benchmark/ips'
empty_array = []
small_array = [1] * 30
bigger_array = [1] * 300
Benchmark.ips do |x|
x.report('empty !empty?') { !empty_array.empty? }
x.report('small !empty?') { !small_array.empty? }
x.report('bigger !empty?') { !bigger_array.empty? }