Skip to content

Instantly share code, notes, and snippets.

View greyblake's full-sized avatar
🇺🇦
#StandWithUkraine

Serhii Potapov greyblake

🇺🇦
#StandWithUkraine
View GitHub Profile
@greyblake
greyblake / gist:1241912
Created September 26, 2011 09:13
Misunderstanding of :inverse_of?
class User < ActiveRecord::Base
has_many :emails, :inverse_of => :user
end
class Email < ActiveRecord::Base
belongs_to :user , :inverse_of => :emails
end
# spec 1
u = User.find(1)
@greyblake
greyblake / inject_VS_each.rb
Created January 17, 2012 09:21
inject, merge, each ...
#!/usr/bin/env ruby
require 'benchmark'
array = (1..10_000).to_a
m = Benchmark.bm(15) do |x|
x.report("inject") do
array.inject({}) do |hash, element|
hash[element] = element
@greyblake
greyblake / actionmailer_bug.rb
Created April 10, 2012 17:09
ActionMailer bug?
# When everything work
class TestMailer < ActionMailer::Base
default :from => "from@example.com", :to => "to@example.com"
def hello
mail do |format|
format.text { render :text => "Hello!" }
format.html { render :text => "<h1>Hello!</h1>" }
end
end
@greyblake
greyblake / art.rb
Created April 27, 2012 14:12
art.rb
class Art
L = 2
MAX_EPOCH = 20
def initialize(n, m, p = 0.45)
@n = n
@m = m
@p = p
@w1 = Array.new(n) { Array.new(m) { 1.0 / (1+n) } }
@w2 = Array.new(m) { Array.new(n) { 1.0 } }
@greyblake
greyblake / bashrc
Created July 7, 2012 16:27
current repo in PS1
# git branch
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
export PS1='\u@\h \[\033[1;34m\]\w\[\033[0m\]\[\033[0;33m\]$(parse_git_branch)\[\033[0m\]$ '
@greyblake
greyblake / index.cgi
Created October 2, 2012 18:22
Serg server
#!/usr/bin/env ruby
require 'cgi'
# Refresh period
PERIOD = 1 # seconds
# Calculate random value from 1 to 10
value = rand(10) + 1
# Override some stuff to detect FacotoryGirl usage out of `it`
module RSpec
module Core
class ExampleGroup
class << self
# redefine to do nothing
def it(*args)
end
class SocialNotifier
include Celluloid
attr_reader :status
def intialize(&block)
@block = block
end
def run
@greyblake
greyblake / show_page.rb
Created November 21, 2013 19:22
My tiny useful method for Capybara webkit driver to see screenshot in one command when I debug
def show_page
filename = "/tmp/shot.png"
driver = page.driver
driver.save_screenshot(filename)
system "eog #{filename}"
end
@greyblake
greyblake / xml_benchmarks.rb
Created September 19, 2014 07:49
XmlMini backend benchmarks
# Result
#
# user system total real
# REXML 8.190000 0.020000 8.210000 ( 8.211740)
# LibXML 1.140000 0.000000 1.140000 ( 1.141131)
# Nokogiri 1.600000 0.000000 1.600000 ( 1.604999)
#
require 'benchmark'