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 / 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'
@greyblake
greyblake / json_benchmark.rb
Created September 19, 2014 08:50
JSON backends comparison
#
# user system total real
# json_gem 1.970000 0.000000 1.970000 ( 1.969385)
# yajl 2.090000 0.000000 2.090000 ( 2.096399)
# oj 1.240000 0.000000 1.240000 ( 1.234021)
# json_pure 23.400000 0.000000 23.400000 ( 23.432088)
#
require 'benchmark'
@greyblake
greyblake / method_visibility_declaration_proposal.rb
Last active August 29, 2015 14:15
Method visibility declaration
# Since ruby 2.1, method definition returns a name of a method as a symbol.
# Example:
def aaa; end # => :aaa
# How we do it currently
class SomeClass
def pub_meth
# ...
end
@greyblake
greyblake / rubocop-detect.sh
Created March 19, 2015 14:35
rubocop-detect
# Finds ruby files that was changed in HEAD against passed branch
# and run rubocop with them. It's supposed to help to detect
# rubocop offenses introduced by YOU.
#
# Examle:
# rubocop-detect develop
function rubocop-detect(){
local branch=${1:-master}
echo -e "\e[1mCOMPARING:\e[0m"
@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