Skip to content

Instantly share code, notes, and snippets.

View DimaSamodurov's full-sized avatar

Dmytro Samodurov DimaSamodurov

View GitHub Profile
Dir['*/'].each do |dir|
root = File.dirname __FILE__
cmd = "cd #{root}/#{dir} && git pull"
puts cmd
`#{cmd}`
end
@DimaSamodurov
DimaSamodurov / capybara-firefox5-test.rb
Created July 7, 2011 15:58
Check if Capybara opens firefox
# coding: utf-8
# Upgrading ruby or firefox selenium tests stopped working.
# Use selenium-webdriver >= 0.2.2 with ruby 1.9.2 and Firefox 5.
require "rubygems"
require "capybara/dsl"
class Test
include Capybara::DSL
def distance(str1, str2)
str1.downcase!
pairs1 = (0..str1.length-2).collect {|i| str1[i,2]}.reject {
|pair| pair.include? " "}
str2.downcase!
pairs2 = (0..str2.length-2).collect {|i| str2[i,2]}.reject {
|pair| pair.include? " "}
union = pairs1.size + pairs2.size
intersection = 0
pairs1.each do |p1|
@DimaSamodurov
DimaSamodurov / untrusted_packages.sh
Created December 27, 2011 13:56
How to Fix ‘Requires installation of untrusted packages’ error in Ubuntu
$sudo apt-get update
#notice line like: "The following signatures were invalid: BADSIG 40976EAF437D05B5 Ubuntu Archive.."
#install public key above
$sudo apt-key adv --recv-key --keyserver keyserver.ubuntu.com 40976EAF437D05B5
#thanks to: http://www.liberiangeek.net/2010/10/fix-requires-installation-untrusted-packages-error-ubuntu-10-10-maverick-meerkat/
@DimaSamodurov
DimaSamodurov / psych_check.rb
Created March 16, 2012 10:42
Check psych compatibility
# encoding: utf-8
require 'erb'
require 'psych'
require 'yaml'
puts YAML # => 'psych'
Dir.glob('**/*.yml').map do |file|
puts file
Psych.load(IO.read file) unless file =~ /cucumber/
end
@DimaSamodurov
DimaSamodurov / chef-redeploy.txt
Created July 24, 2012 16:06
Forse chef-client to redeploy rails app
How to redeploy rails application in case of deployment went wrong.
rm /var/chef/cache/revision-deploys/app
rm file and the latest release directory.
@DimaSamodurov
DimaSamodurov / persistent.rb
Created August 14, 2012 22:59
Consider 'no_proxy' environment variable in Net::HTTP::Persistent (used by Bundler 1.1.5)
require 'net/http'
require 'net/http/faster'
require 'uri'
require 'cgi' # for escaping
require 'http_configuration'
##
# Persistent connections for Net::HTTP
#
# Net::HTTP::Persistent maintains persistent connections across all the
@DimaSamodurov
DimaSamodurov / ews_ntml.rb
Created September 4, 2012 12:04
Connection to exchange web services with NTML auth
# https://github.com/macks/ruby-ntlm
require 'ntlm/http'
require 'open-uri'
require 'net/https'
http = Net::HTTP.new(host, 443)
http.use_ssl = true
http.verify_mode = OpenSSL:SSL::VERIFY_NONE
request = Net::HTTP::Get.new('/ews/Services.wsdl')
@DimaSamodurov
DimaSamodurov / build_tree.rb
Created October 29, 2012 15:45
Build hash style tree from array of nodes.
# Got from: http://stackoverflow.com/questions/8028211/tree-to-array-algorithm-ruby
def build_tree(i, edges)
list = {}
out_nodes = edges.select {|e| e[0] == i}.map {|e| e[1]}.uniq
out_nodes.each {|n| list[n] = build_tree(n, edges)}
list
end
edges = [[1,2],[1,6],[1,9],[2,3],[3,10],[4,7]]
@DimaSamodurov
DimaSamodurov / ruby_meta.rb
Last active December 11, 2015 10:39
Ruby class_eval vs instance_eval
# Two great articles allow to understand Ruby better
# http://yugui.jp/articles/846
# http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/
class A; end
A.instance_eval { define_method(:hoge) { "hoge" } }
A.class_eval { define_method(:fuga) { "fuga" } }
A.instance_eval { def piyo ; "piyo"; end }
A.class_eval { def foo ; "foo"; end }