Skip to content

Instantly share code, notes, and snippets.

@nmk
nmk / foo.rb
Created November 21, 2013 15:06
class Foo
attr_accessor :n
def initialize(i)
@n = i
end
def val
n
end
@nmk
nmk / main.hs
Last active December 23, 2015 12:19
Using a Reader monad to hold a DB handle for Happstack handlers
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
module Main
where
import Control.Monad.Reader
import Data.Data (Data, Typeable)
@nmk
nmk / Ranges.hs
Created September 1, 2013 09:28
Support ranges in postgresql-simple
module Database.PostgreSQL.Simple.Ranges
where
import Control.Applicative
import Data.Attoparsec.Char8
import qualified Data.ByteString as B
data Range a = Range a Bool a Bool | Empty deriving Show
parseRange :: Char -> (B.ByteString -> a) -> Parser (Range a)
@nmk
nmk / Gemfile
Last active December 14, 2015 07:29
Testing inverse of
ruby "2.0.0"
source "https://rubygems.org"
gem "activerecord", "4.0.0.beta1"
gem "sqlite3"
gem "rspec"
@nmk
nmk / eager-load.rb
Created February 21, 2013 05:04
Eager loading associations of the same class
require "bundler"
Bundler.setup
require "active_record"
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Migration.create_table :bugs do |t|
t.string :name
t.integer :reporter_id
@nmk
nmk / gist:4959158
Last active December 13, 2015 18:58
RVM not switching to system ruby
~ $ rvm list
rvm rubies
jruby-1.7.2 [ x86_64 ]
=* ruby-1.9.3-p374 [ x86_64 ]
# => - current
# =* - current && default
# * - default
@nmk
nmk / app.rb
Created March 5, 2012 12:21
Testing headers with capybara Rack::Test browser
require 'sinatra'
get '/' do
%Q{
<html>
<head></head>
<body>
#{request.env['HTTP_ACCEPT_LANGUAGE']}
</body>
</html>
@nmk
nmk / amount-deviation-spec.rb
Created December 13, 2011 06:55
Amount deviation
require 'bigdecimal'
class BigDecimal
def inspect; to_s('F'); end
end
class Account
attr_reader :intial_amount, :balance_changes
@nmk
nmk / big-decimal-test.rb
Created December 5, 2011 14:25
Testing percent summation using BigDecimal values
require 'bigdecimal'
require 'benchmark'
require 'rspec/autorun'
module Calculator
ONE = BigDecimal('1')
ONE_HUNDRED = BigDecimal('100')
DECIMAL_ROUNDING_SCALE = 6 # number of fractional digits to retain
@nmk
nmk / poly_embed.rb
Created October 17, 2011 15:30
Polymorphic embedding in a list
require "mongoid"
require "minitest/autorun"
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('test_polymorphic_embedding')
end
class Condition; end
# A relation consists of a name and a list of arguments