Skip to content

Instantly share code, notes, and snippets.

View drKreso's full-sized avatar

Kresimir Bojcic drKreso

View GitHub Profile
@drKreso
drKreso / gist:1156784
Created August 19, 2011 13:26
Terminal.rb
require 'rexml/parsers/baseparser'
module Kramdown
module Converter
# Converts a Kramdown::Document to Terminal
class Terminal < Html
def convert_blank(el, indent)
@drKreso
drKreso / dependency_injection_example.rb
Created November 20, 2011 22:40 — forked from bravoecho/dependency_injection_example.rb
Flexibility with and without Dependency Injection
require 'minitest/autorun'
class UsTaxCode
def generate(id)
"US-#{id}"
end
end
class BrazilTaxCode
def generate(id)
@drKreso
drKreso / when.rb
Created December 6, 2011 07:05 — forked from avdi/when.rb
Experimenting with Object#when in Ruby
class Object
def when(matcher)
if matcher === self then yield(self) else self end
end
end
# I read it like this : if "when" lambda/proc is true execute block, otherwise return self
#deeply confused
#Proc#=== is equivalent to Proc#call
@drKreso
drKreso / gist:1891958
Created February 23, 2012 09:41
The Gilded Rose Code Kata
require 'guerrilla_patch'
SPECIAL_ITEMS = {
legendary: [ 'Sulfuras, Hand of Ragnaros' ],
one_time_event: [ 'Backstage passes to a TAFKAL80ETC concert'],
better_with_age: ['Aged Brie'],
conjured: ['Conjured Mana Cake']
}
QUALITY_AMOUNT_RULES = [
class MaterialConsumption
include MultidimensionalTable
dimensions :year => [:year_1994, :year_1995],
:city => [:buenos_aires],
:material => [:coal, :potassium]
table_data do
year_1994 do
buenos_aires do
@drKreso
drKreso / gist:2191268
Created March 25, 2012 03:53
This is implementation
module MultidimensionalTable
@context = []
@table_rules = {}
@attributes ||= {}
@index_level = 0
def dimensions=(map)
@dimensions = map
@dimensions.each do |key, value|
value.each do |possible_value|
@drKreso
drKreso / gist:2199562
Created March 25, 2012 20:30
MultidimensionalTable
module MultidimensionalTable
def initialize
set_dimensions(dimensions)
data
end
def set_dimensions(map)
@dimensions = map
@dimensions.each do |key, value|
@drKreso
drKreso / gist:2337113
Created April 8, 2012 12:48
Allocate old
@@ -590,7 +590,7 @@ class SpritesTest < Test::Unit::TestCase
it "should generate a sprite from nested folders" do
css = render <<-SCSS
- @import "nested/*.png";
+ @import "nested/**/*.png";
@include all-nested-sprites;
SCSS
assert_correct css, <<-CSS
@drKreso
drKreso / gist:2337175
Created April 8, 2012 12:57
Allocate_diff
def self.evenly(amount, number_of_slices)
- allocator = Allocate.new
- allocator.amount = amount
- allocator.ratios = (1..number_of_slices).map { |rata| 1.to_d/number_of_slices }
- allocator.divided
+ Allocate.new.tap do |a|
+ a.amount = amount
+ a.ratios = (1..number_of_slices).map { 1.to_d/number_of_slices }
+ end.divided
end
@drKreso
drKreso / Subseq.rb
Last active December 10, 2015 12:28
def subseq(list)
list.drop(1).each_with_object([[list.first]]) do |number, sequences|
(number == sequences.last[-1] + 1) ? sequences.last << number : sequences << [number]
end.max_by(&:count).tap { |sequence| sequence.clear if sequence.count == 1 }
end
puts 'error 1' if subseq([1,0,1,2,3,0,4,5]) != [0,1,2,3]
puts 'error 2' if subseq([5,6,1,3,2,7]) != [5,6]
puts 'error 3' if subseq([2,3,3,4,5]) != [3,4,5]
puts 'error 4' if subseq([7,6,5,4]) != []