Skip to content

Instantly share code, notes, and snippets.

View we4tech's full-sized avatar

Hossain Khan we4tech

View GitHub Profile
@we4tech
we4tech / proc-example.rb
Created July 24, 2012 10:03
Ruby closure Proc example
def say_hi_to(block)
puts "Say hi #{block.call}"
end
say_hi_to Proc.new { "hasan" }
#=> Say hi hasan
@we4tech
we4tech / lambda-example.rb
Created July 24, 2012 10:43
Ruby closure lambda example
l = lambda { |a, b| ... }
l.call('A')
#=> Error need to pass 2 arguments
@we4tech
we4tech / lambda-proc-diff.rb
Created July 24, 2012 10:45
ruby closure lambda proc difference
def whats_your_name?
l = Proc.new { return "Karim" }
l.call
return "Rahim"
end
#=> "Karim"
@we4tech
we4tech / method.rb
Created July 24, 2012 10:47
Ruby closure method
def hola
puts 'hola'
end
def say_hola(block)
block.call
end
say_hola method(:hola)
@we4tech
we4tech / export-mongo-collections.rb
Last active December 11, 2015 05:48
Export mongodb collections and their properties to HTML format
# Export collection name and their properties list in HTML format.
require 'mongo'
include Mongo
# Connect with mongodb server
client = MongoClient.new('localhost', 27017)
# Load database
@we4tech
we4tech / db.bash
Created January 24, 2013 05:59
Simple bash script to quickly import an existing schema to a fixed database or get quick access to mysql console.
#!/bin/bash
echo "---------------------------------------"
echo " Welcome to Quick Schema Importer "
echo "---------------------------------------"
ROOT=$PWD
CONF=""
@we4tech
we4tech / thread_pool.rb
Last active December 16, 2015 19:49
Thread pool, and separate mongo client with each thread.
class ThreadPool
attr_accessor :size, :pool
def logger
BeatDeckMachine::logger
end
def initialize(size)
@size = size
@jobs = Queue.new
@we4tech
we4tech / twitter_client_patch.rb
Created May 31, 2013 06:43
Twitter Client with control loop, If any too many requests error raised. It'll sleep for a certain time period and will wake up and continue from last batch. This is an addition to Twitter::Client 'gem twitter'
# Twitter Client with control loop, If any too many requests error raised.
# It'll sleep for a certain time period and will wake up and continue from last batch.
#
# This is an addition to Twitter::Client 'gem twitter'
module BeatDeckMachine::Scrapers::OAuth::TwitterClientPatch
MAX_ATTEMPTS = 20
extend ActiveSupport::Concern
@we4tech
we4tech / after_refactor_order_service.rb
Last active December 21, 2015 20:39
Just an example how to refactor and separate code and their concerns.
class OrderService
class << self
def find_or_create_purchase_for_order(order, credit_card_id, shipping_fee = 0.0)
user, vendor = order.buyer, order.seller
purchase = Purchase.find_by_user_id_and_order_id(user.id, order.id)
return purchase if purchase.present?
create_purchase order, credit_card_id, shipping_fee
end
@we4tech
we4tech / american_style.rb
Created September 21, 2013 05:37
Pasta in ruby mixin
module AmericanStyle
extend ActiveSupport::Concern
included do
has_many :american_ingredients
end
module ClassMethods
def macaroni_cheese; ... end
end