Skip to content

Instantly share code, notes, and snippets.

View BitOfUniverse's full-sized avatar

Alexey Nikolaev BitOfUniverse

View GitHub Profile
@BitOfUniverse
BitOfUniverse / iffe4.rb
Created March 28, 2019 11:56
Ruby IFFE usage in Rails
class RailsController
helper_method def current_user
User.current
end
end
@BitOfUniverse
BitOfUniverse / iffe3.rb
Created March 28, 2019 11:56
Ruby IFFE as a method parameter
some_method def another_method; end
@BitOfUniverse
BitOfUniverse / iffe2.rb
Created March 28, 2019 11:55
Ruby IFFE with method definition
send def execute_right_now
puts "That's nice!"
end
@BitOfUniverse
BitOfUniverse / iffe.rb
Created March 28, 2019 11:54
Ruby IFFE
(->{ puts "Hey, I'm IIFE" }).()
@BitOfUniverse
BitOfUniverse / trigger_on_change_in_react.js
Created March 14, 2018 17:22
Send onChange event in React
// in React after 15.6
const triggerInputChange = (node, value = '') => {
const inputTypes = [
window.HTMLInputElement,
window.HTMLSelectElement,
window.HTMLTextAreaElement
];
if (inputTypes.indexOf(node.__proto__.constructor) > -1) {
@BitOfUniverse
BitOfUniverse / benchmark_block_passing.rb
Created March 8, 2018 14:45
Ruby benchmark [2.4 vs 2.5]: Lazy Proc allocation for block parameters
require 'benchmark'
def with_block(&block); end
Benchmark.bmbm(10){|x|
x.report("with_block") { 1_000_000.times{ with_block{} } }
}
# Using /Users/alexey/.rvm/gems/ruby-2.4.2
# ➜ ruby ruby benchmark_ruby_block_passing.rb
@BitOfUniverse
BitOfUniverse / iffe.rb
Last active March 7, 2018 15:03
Ruby IIFE
# def returns symbol
# 1
send def me; puts "iffy" end
# 2
method(def me; puts "iffy" end).call
# 3
(->{ puts "iffy" }).()
@BitOfUniverse
BitOfUniverse / ssh.rake
Created March 20, 2017 09:09
SSH to capistrano server
# lib/capistrano/tasks/ssh.rake
#
# Usage: cap staging ssh
desc 'Connects thru SSH to server'
task :ssh do
on roles(:app) do |server|
ssh_command = nil
with_props(server) do |host, user|
ssh_command = "ssh #{user}@#{host}"
end
@BitOfUniverse
BitOfUniverse / db.rake
Created March 15, 2017 10:38 — forked from hopsoft/db.rake
Rails rake tasks for dump & restore of PostgreSQL databases
# lib/tasks/db.rake
namespace :db do
desc "Dumps the database to db/APP_NAME.dump"
task :dump => :environment do
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_dump --host #{host} --username #{user} --verbose --clean --no-owner --no-acl --format=c #{db} > #{Rails.root}/db/#{app}.dump"
end
puts cmd
# Base on this article: https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
free -h
# Make the Swap File Permanent