Skip to content

Instantly share code, notes, and snippets.

View leemour's full-sized avatar

Viacheslav Ptsarev leemour

View GitHub Profile
@leemour
leemour / reset_pk_sequence.rb
Last active May 23, 2018 04:01
Reset Postgres primary key sequence from Rails
start_id = Product.maximum(:id) + 1
table = :products
ApplicationRecord.connection.execute("ALTER SEQUENCE #{table}_id_seq RESTART WITH #{start_id}")
@leemour
leemour / fibonacci.rb
Last active June 7, 2018 07:31
Even Fibonacci Numbers
module Fibonacci
class << self
def sum_of_even_upto(max)
upto(max).inject(0) do |sum, item|
item.even? ? sum + item : sum
end
end
def upto(max)
@leemour
leemour / curry.rb
Created May 16, 2018 03:51 — forked from KamilLelonek/curry.rb
Currying functions in Ruby
[1] (pry) main: 0> add = -> (a, b) { a + b }
=> #<Proc:0x007ffde11d72c0@(pry):1 (lambda)>
# Call proc with two arguments
[2] (pry) main: 0> add.(1, 2)
=> 3
# Call proc with one argument
[3] (pry) main: 0> add.(1)
ArgumentError: wrong number of arguments (1 for 2)
@leemour
leemour / count_by_month.sql
Created May 15, 2018 14:26
Group and count by month SQL query
SELECT count(*), DATE_TRUNC( 'month', created_at ) created_date
FROM businesses
GROUP BY created_date
@leemour
leemour / db.rake
Created May 4, 2018 19:43
Backup all db records older than x
namespace :db do
desc "Backup DB as separate schema"
task :backup_to_schema, [:created_at] => :environment do |t, args|
schema = 'copy'
created_at = args.created_at || 2.years.ago
ApplicationRecord.connection.execute(
"DROP SCHEMA #{schema} CASCADE;
CREATE SCHEMA copy"
)
ActiveRecord::Base.connection.execute "SET search_path TO public, #{schema}"
@leemour
leemour / teaching.rb
Last active April 23, 2018 13:12
Teaching Ruby classes
class Human
def initialize(name)
@name = name
end
def read_book(book, page_number)
page = book.open_page(page_number)
read(page)
end
@leemour
leemour / paste_into_console.js
Created April 20, 2018 11:00
Paste into DigitalOcean console
!function(){function t(){function n(t,e){s=s.concat(RFB.messages.keyEvent(t,e))}var o=e.shift(),s=[],i=o.charCodeAt(),c=-1!=='!@#$%^&*()_+{}:"<>?~|'.indexOf(o),r=XK_Shift_L;c&&n(r,1),n(i,1),n(i,0),c&&n(r,0),rfb._sock.send(s),e.length>0&&setTimeout(t,10)}var e=prompt("Enter text to be sent to console").split("");t()}();
@leemour
leemour / load_jquery.js
Last active February 18, 2018 14:59
Load jQuery in JavaScript console on any web page
fetch('http://code.jquery.com/jquery-latest.min.js').then(response => response.text()).then(text => { eval(text); console.log($) })
@leemour
leemour / rename_file_extensions.sh
Last active February 14, 2018 12:13
Change file extensions. Change from .css.scss to .scss
find app/assets/stylesheets -name "*.css.scss" -exec rename 's/\.css.scss$/.scss/' '{}' \;
#!/bin/sh
for file in $(find ./app/assets/stylesheets/ -name "*.css.scss")
do
git mv $file `echo $file | sed s/\.css//`
done