Skip to content

Instantly share code, notes, and snippets.

Commands examples

If the namespace is not used then the commands will perform on top of the default database.

  bundle exec rake db:create
  bundle exec rake db:migrate 

By using the namespace we are going to use all the configuration for our alternate DB.

require "timeout"
module WaitSteps
extend RSpec::Matchers::DSL
matcher :become_true do
match do |block|
begin
Timeout.timeout(Capybara.default_wait_time) do
sleep(0.1) until value = block.call
@exAspArk
exAspArk / gist:d81e5d7b6ad1bdd3bcf6
Created October 31, 2014 16:50
Immutable deep hash merge (ruby)
def deep_merge(hash1, hash2)
result = hash1.dup
hash2.keys.each do |key|
if hash2[key].is_a?(Hash) && hash1[key].is_a?(Hash)
result[key] = deep_merge(hash1[key], hash2[key])
else
result[key] = hash2[key]
end
end
@exAspArk
exAspArk / hacks.rb
Last active August 29, 2015 14:08 — forked from conf/monkey_patches.rb
Get rid of TZInfo::AmbiguousTime exception
# config/initializers/hacks.rb
Dir[Rails.root.join("lib/hacks/**/*.rb")].each { |file| require file }
@exAspArk
exAspArk / state_machine.rb
Last active February 22, 2016 22:28
HACK StateMachine gem to make it work with ActiveRecord 4.2
# HACK to enable using protected methods from Rails 4.2 and initial states as default attributes for ActiveRecord
module StateMachine
module Integrations
module ActiveModel
public :around_validation
end
module ActiveRecord
public :around_save
@exAspArk
exAspArk / friday_deploy_cap2.rb
Last active November 14, 2020 23:13
Friday deploy script for Capistrano
# Capistrano 2
before "deploy", "friday:good_luck"
namespace :friday do
friday_jumper = %{
┓┏┓┏┓┃
┛┗┛┗┛┃⟍ ○⟋
┓┏┓┏┓┃ ∕ Friday
┛┗┛┗┛┃ノ)
@exAspArk
exAspArk / ruby_heart_print.rb
Last active August 29, 2015 14:15
Prints heart with ruby
ruby_print = ->(a, b = 28, c = 1) { puts ("ruby" * a).center(b) * c } ; [[2, 14, 2], [3, 14, 2], 7, 7, 7, 6, 5, 4, 3, 2, 1].map { |i| ruby_print.call(*i) }
rubyruby rubyruby
rubyrubyruby rubyrubyruby
rubyrubyrubyrubyrubyrubyruby
rubyrubyrubyrubyrubyrubyruby
rubyrubyrubyrubyrubyrubyruby
rubyrubyrubyrubyrubyruby
rubyrubyrubyrubyruby
rubyrubyrubyruby
require "net/http"
def start_server
# Remove the X to enable the parameters for tuning.
# These are the default values as of Ruby 2.2.0.
@child = spawn(<<-EOC.split.join(" "))
XRUBY_GC_HEAP_FREE_SLOTS=4096
XRUBY_GC_HEAP_INIT_SLOTS=10000
XRUBY_GC_HEAP_GROWTH_FACTOR=1.8
XRUBY_GC_HEAP_GROWTH_MAX_SLOTS=0
@exAspArk
exAspArk / gist:c325bb9a75dcda5c8212
Last active July 21, 2022 08:46
Elasticsearch: calculating user sessions with Map/Reduce (Ruby)
# The same algorithm which is used in Google Analytics (https://support.google.com/analytics/answer/2731565?hl=en):
# Time-based expiry (including end of day):
# - After 30 minutes of inactivity
# - At midnight
# Enable dynamic scripting for Groovy (https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#_enabling_dynamic_scripting)
# ! WARNING: please read about security first
# Usage:
#
@exAspArk
exAspArk / gist:42bdb62751a9300ee6d7
Created December 21, 2015 09:42
Screen / Tmux (in iTerm 2)
ssh login@server.example.com -t "screen -DR session_name"
tmux -CC attach -t session_name || tmux -CC new -s session_name