Skip to content

Instantly share code, notes, and snippets.

# example
# date = Date.today
# ExchangeWithFallback.new(
# SynchronizedRates.new(
# DoubleConvertThrough.new('RUB',
# InverseRatesFor.new( {from: 'RUB'},
# RatesUpdatedWithFallback.new(
# ActiveRecordRates.new(CurrencyRate.where(date: date, bank: 'cbr')),
# LazyRates.new { CentralBankOfRussia.new.update_rates(date) } )))))
@codesnik
codesnik / nand.rb
Created August 12, 2012 19:43
nand logic
require 'rubygems'
require 'rspec'
describe "nand logic" do
# false false true
# false true false
# true false false
# true true false
def nand(a, b)
@codesnik
codesnik / and_thats_why.rb
Created May 24, 2012 17:40
string range inclusion test
# AND THAT'S WHY:
# it's logical that "flattened" array should work the same as original range
# but that test fails on ruby1.8.7
# so 1.9 ruby uses search on String#upto, (and String.succ in it) and iterates on all the items in range
require 'test/unit'
class RangeInclusionTest < Test::Unit::TestCase
def test_range_inclusion
assert !('a0'..'b9').include?('aa')
#require 'rubygems'
#require 'rspec'
def evaler(expr)
token = expr.shift
case token
when '*'
evaler(expr) * evaler(expr)
when '+'
evaler(expr) + evaler(expr)
require 'rubygems'
require 'rspec'
def evaler(expr)
token = expr.shift
case token
when '*'
evaler(expr) * evaler(expr)
when '+'
evaler(expr) + evaler(expr)
# encoding: utf-8
module FetchNested
# Fetches deep nested hash value by its dot-separated path
#
# hash = {'foo' => {'bar' => {'baz' => 3}}}
# hash.nested('foo.bar.baz')
# => 3
# hash.nested('foo.key')
# => nil
# hash.nested('foo.key', 5)
@codesnik
codesnik / gist:2035501
Created March 14, 2012 10:02
Hash lvalue slice monkey patch
# lvalue slicing of Hashes
# hash = {foo: 1, bar: 2, baz: 3}
# hash[:foo, :bar] == [1, 2]
#
# hash[:foo, 3] = 6,7
# hash == {foo: 6, bar: 2, baz: 3, 3 => 7}
#
# strange enough, I haven't noticed any slowdown on rails startup.
class Hash
alias oldbracket []
@codesnik
codesnik / amadeus_workflow.md
Created February 28, 2012 11:02
amadeus wokflow

SEARCH & DISPLAY

get session from pool

get customer input (departure, destination, dates, passengers count)

# search for recommendations conforming to customer input
do Fare_TravelBoardSearch
# encoding: utf-8
# параметры от форм приходят в виде string-ов и массивов
# этот модуль призван помогать конвертировать значения у обычных аксессоров
# тем же способом, что это делает и активрекорд
#
# пока поддерживается только cast_to_boolean
module CastingAccessors
def cast_to_boolean accessor
define_method "#{accessor}_with_casting=" do |value|
require 'rubygems'
require 'rspec'
def includes
end
def includes_only
end
describe "#includes" do