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 / gist:958862
Created May 6, 2011 12:26
Autotest integration with libnotify on GNU/Linux
#! /usr/bin/env ruby
require 'autotest/timestamp'
IMAGES_ROOT = File.expand_path("~/.dotfiles/autotest_images/small")
module Autotest::LibNotify
def self.notify(title, msg, urgency=:normal)
img = File.join(IMAGES_ROOT, "#{title.downcase}.png")
@vderyagin
vderyagin / class_accessors.rb
Created May 18, 2011 05:12
Gain class-level accessor methods by including module in your class
#! /usr/bin/env ruby
module ClassAccessors
def self.included(klass)
class << klass
def class_attr_reader(*args)
args.each do |attr|
@vderyagin
vderyagin / Array#rotations.rb
Created June 18, 2011 09:27
Array#rotations method for Ruby
#! /usr/bin/env ruby
class Array
def rotations
enum = Enumerator.new do |yielder|
r = dup
length.times do
yielder << r
r = r.rotate
@vderyagin
vderyagin / Guardfile.rb
Created August 4, 2011 10:37
Guardfile I use for new Rails apps
guard 'livereload' do
watch(%r{app/.+\.(erb|haml)})
watch(%r{app/helpers/.+\.rb})
watch(%r{(public/|app/assets).+\.(css|js|html)})
watch(%r{(app/assets/.+\.css)\.s[ac]ss}) { |m| m[1] }
watch(%r{(app/assets/.+\.js)\.coffee}) { |m| m[1] }
watch(%r{config/locales/.+\.yml})
end
guard 'spork', :wait => 30, :rspec => true, :rspec_env => { 'RAILS_ENV' => 'test' } do
@vderyagin
vderyagin / enumerables.rb
Created September 8, 2011 04:58
print names of all classes, that include Enumerable module
ObjectSpace.each_object(Class) do |klass|
puts klass.name if klass < Enumerable
end
class ColorString < String
ABORT = "\e[0m"
ESC_SQS = {
:red => "\e[1m\e[31m",
:dark_red => "\e[31m",
:green => "\e[1m\e[32m",
:dark_green => "\e[32m",
:yellow => "\e[1m\e[33m",
:dark_yellow => "\e[33m",
:blue => "\e[1m\e[34m",
@vderyagin
vderyagin / Makefile
Created October 12, 2011 08:29
Makefile, that compiles all *.c files in directory, each to separate binary
PROGS = $(patsubst %.c, %, $(wildcard *.c))
all:
make $(PROGS)
clean:
rm -f $(PROGS)
@vderyagin
vderyagin / rcat
Created October 19, 2011 05:55
rcat: cat(1) partial implementation in ruby (passes https://gist.github.com/1293709)
#!/usr/bin/env ruby
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.on('-n') { options[:number_all_lines] = true }
opts.on('-b') { options[:number_nonblank_lines] = true }
opts.on('-s') { options[:squeeze_blank_lines] = true }
@vderyagin
vderyagin / fizzbuzz.rb
Created October 29, 2011 08:32
FizzBuzz
1.upto(100) do |number|
str = ''
str << 'Fizz' if (number % 3).zero?
str << 'Buzz' if (number % 5).zero?
str << number.to_s if str.empty?
puts str
end
@vderyagin
vderyagin / stack.rb
Created November 17, 2011 08:07
stack data structure implementation in ruby
require 'forwardable'
class Stack
OverflowError = Class.new StandardError
UnderflowError = Class.new StandardError
extend Forwardable
def_delegators :@ary, :empty?, :size