Skip to content

Instantly share code, notes, and snippets.

@kgrz
kgrz / euler_9.rb
Created August 15, 2012 16:45
Project Euler problem 9. Using formula.
require 'benchmark'
result = []
Benchmark.bm(7) do |reports|
reports.report do
(2..500).each do |x|
(2..500).each do |y|
next unless x > y
a = [x*x-y*y, 2*x*y, x*x+y*y]
next unless (a[0] + a[1] + a[2] === 1000)
next unless (a[0]*a[0]+a[1]*a[1] === a[2]*a[2])
@kgrz
kgrz / mongoid3_sinatra_config.rb
Created August 22, 2012 17:04
A Better Mongoid Sinatra configuration implementation. Using settings hash for db options sucks!
# This is a modification of the blogpost on how to use Mongoid and Sinatra.
# Here is the original blogpost http://www.garrensmith.com/2010/09/11/Mongoid-sinatra.html
# The modification lets you use Mongoid 3.0 with the new Moped driver
# The new Moped driver uses the Sessions component to define the MongoDB connection rather
# than the Mongo::Connection.new that is used in the case of the default Ruby driver.
# Moped::Session.new vs Mongo::Connection.new
# Mongoid.load!(yaml_config.yml) is used to build the config hash. Alternatively, the hash can
# be built manually by using Mongoid.config {|config| ...} syntax. The config.sessions hash
@kgrz
kgrz / 1_N_embedded_error.rb
Created September 8, 2012 02:33
Mongoid unable to create 1-N referenced docs in embedded documents
class Email
include Mongoid::Document
field :emailid, type: String
embeds_many :account
end
class Account
include Mongoid::Document
field :number, type: Integer
field :openedon, type: DateTime
@kgrz
kgrz / sh_gem_awk_rbenv
Created September 18, 2012 10:06
Shell script experiments for exporting and installing gems for different versions of Ruby
TL;DR
Putting it all together: Make sure you are using the Ruby version that you need to export from
$ gem list > ~/gemlist && rbenv local rbx-1.2.4 && \
gem install `awk -F' ' '{print $1}' ~/gemlist` && rm ~/gemlist
How I got there:
By exporting to a file:
@kgrz
kgrz / each_with_symbol.rb
Created October 16, 2012 08:08
Enumerating the contents of a file split with a symbol
# Enumerable module provides most of the list traversal classes, some of which are mixed-in to File class.
# #each_line, #each_slice etc are such methods.
# What if you need to split against a custom symbol. This is particularly useful while processing reports or logs.
# For instance, one of the reports I get is see is of this format:
# \e!C\r
# <some header data>
# -----------------------------------------------------------------------
# Name Account Address DoB
@kgrz
kgrz / faraday-em-http.rb
Created November 10, 2012 16:01 — forked from igrigorik/faraday-em-http.rb
using Faraday with EM-Synchrony & EM-Http
require 'faraday'
require 'net/http'
require 'pp'
# Repos:
# https://github.com/technoweenie/faraday
# https://github.com/pengwynn/faraday_middleware
# Blog posts:
# http://adventuresincoding.com/2010/09/writing-modular-http-client-code-with-faraday
@kgrz
kgrz / faraday_json_middleware.rb
Created November 10, 2012 17:54
Using Faraday and making CouchDB return JSON. Simple middleware
# this simple plumbing makes sure that the return data is in the form of a hash.
require 'faraday'
class Main
attr_reader :conn
def initialize
@conn = Faraday.new "http://127.0.0.1:5984/somedb" do |faraday|
faraday.adapter :em_http
faraday.builder.insert(0, JSONMiddleware)
@kgrz
kgrz / couch_erlang_trouble.txt
Created December 2, 2012 17:00
CouchDB Erlang issues. Troubleshooting notes
If the error has "emfile" in the reason, it most probably has to do with the ulimit.
If the error is a TCP driver error saying that max file descriptors can be only a certain value, it has to do with the compilation of Erlang. This link: http://boorad.github.com/2009/07/09/mac-kpoll.html has info.
Setting the FD_SETSIZE to 8192 worked and limited the TCP driver connections to 8192.
Setting it to 81920 makes CouchDB fail to start.
^
| That worked. The reason why couch didnt start up was the setting of mochiweb acceptor pool to an atrocious 81000!
things to look out for:
@kgrz
kgrz / soundcloud.rb
Last active December 11, 2015 01:29 — forked from anonymous/soundcloud.rb
Soundcloud Sinatra authorization example. Created the anonymous gist by mistake and hence, forked it here. This is an example to explain the auth-flow and probably has bugs.
# Soundcloud, after authorization, redirects to a pre-registered URL
# with the user's code, access_token embedded in the URL which may
# be an issue!
# Alternatively, modify the #authorize_url() method such that the url
# that gets generated has a parameter "response_type" of "code" instead
# of "code_and_token" which is the default.
require 'sinatra'
require 'soundcloud'
@kgrz
kgrz / sinatra.rake
Last active December 16, 2015 09:38
Create a directory structure for Sinatra using rake task. Not the most robust implementation but works.
# ~/.rake is your global rake directory. Rake knows the tasks you write there.
# Create a rake file, say, sinatra.rake
desc "Create a directory structure for Sinatra classic app"
task :create_sinatra do
sh "mkdir public"
sh "touch app.rb"
sh "mkdir public/js"
sh "mkdir public/css"
sh "mkdir public/images"