Skip to content

Instantly share code, notes, and snippets.

@kucaahbe
kucaahbe / tiny_benchmark.lua
Created February 18, 2021 23:02
lua benchmark
local function run(self, name, f)
local start = os.clock()
f()
local now = os.clock()
table.insert(self.result, { name=name, time=now-start })
end
local function report(self)
local data = ''
@kucaahbe
kucaahbe / spec_support_time_precision_patch.rb
Created April 18, 2019 11:55
Rails time comparison workaround
module RemoveTimePrecisionGranularity
def self.included(mod)
mod.class_eval do
extend ClassMethods
alias_method :orig_equals, :==
end
end
module ClassMethods
def equals_method_ignore_1_second_granularity!
@kucaahbe
kucaahbe / mysql
Last active January 17, 2022 12:05
rails db -p use mycli as MySQL client (shell wrapper for https://www.mycli.net/)
#!/bin/bash
# usage:
# save this file into $HOME/bin/mysql (don't forget to make it executable)
# and make sure $HOME/bin is in PATH environment variable before folder where original mysql binary is located.
# for instance mysql binary is here: /usr/local/bin/mysql, than PATH should be like:
# /home/user/bin:/usr/local/bin:...
set -e
declare -a args=()
@kucaahbe
kucaahbe / simple_template.js
Created July 27, 2017 13:10
simple javascript string template engine
/**
Usage:
var engine = SimpleJsTemplator({
value: '{pattern}'
})
engine.compile('string with {pattern}', { value: 'PATTERN' })
will produce:
'string with PATTERN'
*/
var SimpleJsTemplator = (function () {
@kucaahbe
kucaahbe / gist:1416185d44ca73db6f4b
Created June 8, 2015 11:40
Rails old-style configs to new style rename
sed -i.bak -E "s/YAML\.load_file\(Rails\.root\.join\('config\/(.+)\.yml'\)\)\[Rails\.env\]/Rails.application.config_for('\1')/" $(git grep -l YAML.load_file)
@kucaahbe
kucaahbe / array.rb
Created February 12, 2015 11:31
WFT?
irb(main):001:0> a = []
=> []
irb(main):002:0> a << 1
=> [1]
irb(main):003:0> a << a
=> [1, [...]]
irb(main):004:0> a
=> [1, [...]]
irb(main):005:0>
@kucaahbe
kucaahbe / time_and_space_1.rb
Created January 29, 2015 15:06
Time and Space
# version 1
class Space
end
class Time
def self.and smth
end
end
Time.and Space
@kucaahbe
kucaahbe / _enable_hstore.rb
Created August 6, 2014 14:23
enable hstore in postgres (requires user to be superuser)
class EnableHstore < ActiveRecord::Migration
def up
enable_extension :hstore
rescue ActiveRecord::StatementInvalid => maybe_hstore_exception
if maybe_hstore_exception.original_exception.is_a? PG::UndefinedFile
STDERR.puts <<MSG
*****************************************************
Exception occured during enabling "hstore" extension.
@kucaahbe
kucaahbe / capybara_ignore_alert_after_scenario.rb
Created March 21, 2014 15:02
BROKEN: cucumber with capybara: if scenario failed and left modal alert after self: accept it and prevent other scenarios failing
# features/support/capybara_ignore_alert_after_scenario.rb
# if scenario failed and left modal alert after self
# accept it and prevent other scenarios failing
After '@javascript' do |scenario|
if scenario.failed?
# exception for modal dialogs
# http://rdoc.info/gems/selenium-webdriver/Selenium/WebDriver/Error/UnhandledAlertError
if scenario.exception.instance_of?(Selenium::WebDriver::Error::UnhandledAlertError)
page.driver.browser.switch_to.alert.accept
@kucaahbe
kucaahbe / deploy.sh
Created March 3, 2014 12:56
mina deploy wrapper script
#!/bin/bash
# removes from PATH sections with `rbenv`
PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/rbenv/ {next} {print}'`
MINA=`which mina`
[ -x "$MINA" ] || (echo "mina not found: installing mina gem" && sudo gem install mina)
mina $*