Skip to content

Instantly share code, notes, and snippets.

View vderyagin's full-sized avatar

Victor Deryagin vderyagin

  • Ternopil, Ukraine
View GitHub Profile
@vderyagin
vderyagin / re.rb
Created January 26, 2012 11:00
trick with Regexp and to_proc
class Regexp
def to_proc
Proc.new { |arg| arg =~ self }
end
end
# select behaves like as Enumerable#grep:
%w(foo bar baz boo fooz goo).select &/oo/ # => ["foo", "boo", "fooz", "goo"]
@vderyagin
vderyagin / longlines.rake
Created August 9, 2012 09:58
rake task for reporting long lines is source files
desc 'Locate lines of code, that are too long.'
task :longlines do
pattern = ENV['PATTERN'] || '**/*.rb'
length_limit = ENV['LENGTH_LIMIT'] || 80
ignore_comments = (ENV['IGNORE_COMMENTS'] == 'false' ? false : true)
Dir.glob pattern do |file|
File.foreach(file).with_index(1) do |line, index|
if file.end_with?('.rb') && ignore_comments
next if line =~ /^\s*#/
@vderyagin
vderyagin / network-test.rb
Created August 23, 2012 12:34
Test network by investigating connection to servers in different geographical locations.
#!/usr/bin/env ruby
=begin
Test network by investigating connection to servers in different geographical
locations.
Prints results to stdout.
Shells out to mtr(1) (http://www.bitwizard.nl/mtr/).
@vderyagin
vderyagin / foo.el
Created September 23, 2012 10:00
RFC-3092 compatible metasyntactic variable name generator.
(eval-when-compile
(require 'cl-lib))
(defun foo (count)
"Inserts at point position COUNT metasyntactic variable names separated by spaces.
Compatible with RFC-3092."
(interactive "p")
(insert
(mapconcat
'identity
@vderyagin
vderyagin / .rspec
Created November 4, 2012 11:48
spec_helper for use with rspec
--color
--format progress
--order random
@vderyagin
vderyagin / fizz_buzz.go
Created November 13, 2012 19:14
FizzBuzz in Go
package main
import "fmt"
func main() {
fizz_buzz()
}
func fizz_buzz() {
for num := 1; num <= 100; num++ {
@vderyagin
vderyagin / fib.rb
Created November 14, 2012 12:37
cached fibonacci sequence generator in ruby
#! /usr/bin/env ruby
require 'forwardable'
require 'minitest/autorun'
class Fibonacci
extend Forwardable
def_delegator :generator, :take
@vderyagin
vderyagin / helloworld.go
Created November 19, 2012 08:22
concurrent "Hello World" in Golang
package main
import "fmt"
/*
concurrent hello world
*/
func main() {
sayHello := make(chan bool)
@vderyagin
vderyagin / git-churn.rb
Created November 19, 2012 08:25
git churn
#! /usr/bin/env ruby
IO.popen('git log --name-only --pretty=format:""')
.readlines
.map(&:chomp)
.reject(&:empty?)
.select { |file| File.exists?(File.expand_path(file, ENV['GIT_WORK_TREE'])) }
.each_with_object(Hash.new(0)) { |file, changes| changes[file] += 1 }
.to_a
.sort_by(&:last)
@vderyagin
vderyagin / quick_sort.rb
Created November 28, 2012 12:44
quick sort implementation in ruby
def quick_sort(list)
return list.dup unless list.size > 1
x = list.sample
h = list.group_by { |item| item <=> x }
h.default = []
less, equal, more = h[-1], h[0], h[1]