Skip to content

Instantly share code, notes, and snippets.

@Sam-Serpoosh
Sam-Serpoosh / spec_helper_lite.rb
Created June 1, 2012 10:51
a handy method for requiring all ruby files in a folder which is in rails app folder (like controllers, views, helpers, etc.) really useful in rails specs (for me at least). it can be in a spec_helper_lite file in spec folder.
def require_folder(folder_name_in_app)
Dir[File.expand_path(File.dirname(__FILE__) + "/../app/" + folder_name_in_app + "/*.rb"].each { |file| require file }
end
@Sam-Serpoosh
Sam-Serpoosh / Gemfile
Created June 4, 2012 16:14
error in blog sample app in @avdi's Objects on Rails with nulldb setup!!!
source 'http://rubygems.org'
gem 'rails', '3.0.0'
gem 'sqlite3-ruby', :require => 'sqlite3'
group :development do
gem 'rspec-rails', '2.0.1'
end
group :test do
@Sam-Serpoosh
Sam-Serpoosh / current_version_sign_in_out_spec.rb
Created July 17, 2012 21:10
a simple sign_in & sign_out machinery for an app I'm currently working on. I try to follow good OO design & separation of concerns, etc. & extract domain objects as much as possible & have fast-isolated tests. But can't stub simple methods of a module in
module SessionsHelper
include SignInOut
def create_cookie(user_id, user_salt); end
def remember_token; end
def delete_cookie; end
end
@Sam-Serpoosh
Sam-Serpoosh / some_domain_logic_spec.rb
Created July 27, 2012 22:44
This is a little helper method which take the path of a file relative to rails root directory and "require" that file in the test. Comes handy for the isolated tests in a rails project. Interestingly enough, I wrote this via TDD too!
#you can use this easily like the following in your spec files:
reuqire_relative "../spec_helper_lite"
reuqire_relative_to_root "lib/some_domain_logic"
describe "some domain logic" do
#specs will be here then
end
@Sam-Serpoosh
Sam-Serpoosh / web_based_file_explorer.rb
Created October 25, 2012 22:24
Simple webserver which let you explore your files and directories of your system in a browser!
require 'socket'
HTTP_HEADER = "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
CURRENT_DIR = "."
SPACE_IN_PARAM = "%20"
def file_explorer
webserver = TCPServer.new("localhost", 2000)
loop do
Thread.start(webserver.accept) do |session|
@Sam-Serpoosh
Sam-Serpoosh / closest_to_zero.rb
Created November 14, 2012 05:09
Closest to zero! Just a short program for fun!
class Closest
MAX = 2000000000000
def to_zero(input)
raise IllegalArgumentException if input.nil? || input.empty?
closest_to_zero = MAX
@least_distance = MAX
input.each do |num|
if closer_to_zero(num)
@Sam-Serpoosh
Sam-Serpoosh / pair-car-cdr.lisp
Created November 20, 2012 04:54
Just a very simple program from SICP book both in Lisp and Ruby to show how much more elegant the code will be when the philosophy of "Code as Data" exist in a language!
(define make-pair (a, b)
(lambda (pick)
(cond (= pick 1) a)
(cond (= pick 2) b)))
(define car (x) (x 1))
(define cdr (x) (x 2))
@Sam-Serpoosh
Sam-Serpoosh / mutator.rb
Created January 10, 2013 00:57
Very simple Mutator for the concept of Mutation-Testing in Ruby! Just for fun :)
class Mutator
def initialize(ruby_file)
@ruby_file = ruby_file
end
def run_test
spec_file_name = @ruby_file.split(/\./)[0]
system("rspec #{spec_file_name}_spec.rb")
end
@Sam-Serpoosh
Sam-Serpoosh / swap_pairs.clj
Created July 10, 2013 01:58
Cool way of swapping pairs in a collection in Clojure!!!
(defn swap-pairs [items]
(into (empty items)
(interleave (take-nth 2 (drop 1 items))
(take-nth 2 items))))
(swap-pairs [1 2 3 4 5 6]) ;=> [2 1 4 3 6 3]
@Sam-Serpoosh
Sam-Serpoosh / bing_search.rb
Last active February 4, 2016 17:30
Getting results from Bing Search new API! (Azure data market key approach)
require 'net/http'
require 'json'
module Bing
class Search
BASE_URL = "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Composite?"
SINGLE_QUOTE_ENCODED = "%27"
def initialize(api_key)
@api_key = api_key