Skip to content

Instantly share code, notes, and snippets.

View gouravtiwari's full-sized avatar

Gourav Tiwari gouravtiwari

View GitHub Profile
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")
@gouravtiwari
gouravtiwari / include-extend-idiom.rb
Created August 31, 2013 05:01
include-extend-idiom
module Goodness
def great?
@language == 'Ruby'
end
def self.included(base)
base.extend ClassMethods
def interesting
'there are thousands of spoken languages around the world'
@gouravtiwari
gouravtiwari / fibered_prismatic.rb
Last active December 22, 2015 02:28
fibered prismatic shapes
# Prism shapes - naive implementation
counter = 1
stars = 5
(1..20).each do |row|
if counter == stars * 2 - 1
counter = 1
else
if counter > stars
puts '*'*(stars * 2 - counter)
@gouravtiwari
gouravtiwari / turtle.rb
Last active December 22, 2015 02:29
turtuled
# Turtle shapes
# Not at all necessory, but just to show that you can define attr_accessor from module as well
module AttrLoader
def attr_loader(*attrs)
attr_accessor *attrs
end
end
@gouravtiwari
gouravtiwari / splitter_and_parser_using_threads.rb
Last active December 22, 2015 10:48
Split CSVs without parsing and then parse them using Ruby threads, a continuation of http://grosser.it/2011/08/31/splitting-1-big-csv-file-into-multiple-smaller-without-parsing-it/
#http://grosser.it/2011/08/31/splitting-1-big-csv-file-into-multiple-smaller-without-parsing-it/
require 'rubygems'
require 'rake'
# split giga-csv into n smaller files
def self.split_csv(original, file_count)
header_lines = 1
lines = `cat #{original} | wc -l`.to_i - header_lines
lines_per_file = (lines / file_count) + header_lines
header = `head -n #{header_lines} #{original}`
@gouravtiwari
gouravtiwari / attr_accessible_solution_rails_3_and_4.rb
Created September 29, 2013 16:00
Fix for mass assignment for rails engine gem to work on rails-3 and rails-4 both
# If it is Rails-3 then using attar_accessible in model:
def self.needs_attr_accessible?
Rails::VERSION::MAJOR == 3
end
if needs_attr_accessible?
attr_accessible :list_of_attributes
end
@gouravtiwari
gouravtiwari / passenger_for_ruby_2.0.sh
Created September 30, 2013 15:55
passenger with nginx setup
gem install passenger
passenger-install-nginx-module
sudo -E /usr/local/bin/ruby /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.19/bin/passenger-install-nginx-module
vi /opt/nginx/conf/nginx.conf
# Add in http block:
# passenger_root /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.19;
# passenger_ruby /usr/local/bin/ruby;
@gouravtiwari
gouravtiwari / jenkins_setup.sh
Last active April 24, 2019 21:00
Jenkins setup on SUSE
# 1. Install jenkins
sudo zypper addrepo http://pkg.jenkins-ci.org/opensuse/ jenkins
sudo zypper install jenkins
# You would be asked something like this:
# Do you want to reject the key, trust temporarily, or trust always? [r/t/a/?] (r): t
# Overall download size: 115.9 MiB. After the operation, additional 159.9 MiB will be used.
# Continue? [y/n/?] (y): y
# 2. Start jenkins
sudo /etc/init.d/jenkins start
require 'csv'
class Exporter
DEFAULT_EXPORT_TABLES = [ Invoice, InvoiceItem, Item, Merchant, Transaction, User ]
DESTINATION_FOLDER = "tmp/"
def self.export_tables_to_csv(tables = DEFAULT_EXPORT_TABLES)
tables.each { |klass| export_table_to_csv(klass) }
end
@gouravtiwari
gouravtiwari / msql-rhel-fix
Last active December 30, 2015 20:09
mysql rhel error
Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/servername.pid)
OR
Starting MySQL.The server quit without updating PID file (/[FAILED]mysql/servername.pid).
sestatus
echo 0 > /selinux/enforce
sestatus