Skip to content

Instantly share code, notes, and snippets.

Avatar

Arnab Deka arnab

  • AWS
  • Vancouver, BC, Canada
View GitHub Profile
View output.txt
$ ruby titleizer.rb
Using iterative approach: ["alexander", "the", "great"]
Using recursive approach: ["alexander", "the", "great"]
@arnab
arnab / rails-find-joins.rb
Created Nov 2, 2009
If you want to load data from 2 tables (with a belongs_to/has_many relation)
View rails-find-joins.rb
metrics_with_inputs = Revision.find(
:all,
:conditions => { :id => self.revisions.map { |r| r.id } },
:select => 'revisions.metric, revisions.snap_date, revisions.updated_at,
inputs.author, inputs.as_of_date, inputs.units',
:joins => 'join inputs on inputs.revision_id = revisions.id',
:order => 'revisions.metric, revisions.snap_date, revisions.updated_at'
)
View columnize.rb
def columnize(arr, columns)
size_in_group = arr.size / columns
if arr.size % columns != 0
size_in_group += 1
end
arr_grouped = [ ]
size_in_group.times do |m|
columns.times do |i|
arr_grouped[i] ||= []
@arnab
arnab / sort_enums_by_given_custom_order.rb
Created Dec 31, 2009
Sort an enumerable (animals) given a specific order (animal_class_order). Important Lines: 22-24
View sort_enums_by_given_custom_order.rb
animals = [
{ :name => 'Frog', :class => 'Amphibian' },
{ :name => 'Butterfly', :class => 'Arthropod' },
{ :name => 'Eagle', :class => 'Bird' },
{ :name => 'Seahorse', :class => 'Fish' },
{ :name => 'Dog', :class => 'Mammal' },
{ :name => 'Man', :class => 'Mammal' },
{ :name => 'Crocodile', :class => 'Reptile' },
]
# Note that Fish and Reptiles are missing from the following
@arnab
arnab / russian-peasant-multiplication.rb
Created Jan 29, 2010
Simple implementation of Russian Peasant Multiplication with tests
View russian-peasant-multiplication.rb
# http://mathforum.org/dr.math/faq/faq.peasant.html
# http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication
module RussianPeasantMultiplication
def russian_peasant_multiply(b)
numbers_to_add = []
a, b = [self, b].sort #So we have the smaller number as the first
negative_operands = [a, b].select { |n| n < 0 }
result_should_be_negative = negative_operands.size.odd? # or negative_operands.size == 1
@arnab
arnab / top_n_tags.rb
Created Feb 26, 2010
Given lots of tags, keep a running count of the top-n
View top_n_tags.rb
require "pp"
class TagManager
def initialize(top_tags_to_keep)
@all_tags = Hash.new(0)
@top_tags = []
@top_tags_to_keep = top_tags_to_keep
end
def search(tag)
View umpiring-schdeule-generator.rb
#!/usr/bin/env ruby -wKU
players = "Parag
Arnab
Vinoth
Sreeram
Ajay
Nazeer
Swapnil
Sridhar
View greplin-challenge-level-1.rb
require "pp"
class String
def all_substrings
substrs = []
start_index, end_index = [0, self.length - 1]
start_index.upto end_index do |i|
i.upto end_index do |j|
# puts "#{self}[#{i}, #{j}] => #{ss}"
substrs << self[i..j]
@arnab
arnab / sdb_put_attributes_with_fog.rb
Created Oct 18, 2010
SimpleDB put_attributes using fog
View sdb_put_attributes_with_fog.rb
require "fog"
CREDS_FILE = 'Somewhere'
DOMAIN = 'Dogs'
ATTRIBS_TO_CHANGE = {
'color' => 'white'
}
OPTIONS = {
:replace => ['color'],
:expect => {
@arnab
arnab / running-fog-specs.sh
Created Nov 16, 2010
fog spec errors (756 in the whole suite)
View running-fog-specs.sh
ArgumentError in 'Fog::Vcloud power_on with a vapp uri that doesn't exist should == {:type=>"application/vnd.vmware.vcloud.org+xml", :name=>"Boom Inc.", :href=>"https://fakey.com/api/v0.8/MockOrganization/2174953160"}'
Unrecognized arguments: username, module, password, versions_uri
/Users/arnab/code/github/fog/lib/fog/core/service.rb:141:in `validate_arguments'
/Users/arnab/code/github/fog/lib/fog/core/service.rb:43:in `new'
/Users/arnab/code/github/fog/spec/vcloud/terremark/ecloud/requests/../../../spec_helper.rb:276:
You can’t perform that action at this time.