Skip to content

Instantly share code, notes, and snippets.

@gouthamvel
gouthamvel / rspec-syntax-cheat-sheet.rb
Created June 22, 2011 09:34 — forked from dnagir/rspec-syntax-cheat-sheet.rb
RSpec 2 syntax cheat sheet by example
# 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")
@gouthamvel
gouthamvel / timespan.rb
Created July 21, 2011 11:25
simple timespan class for operations on time span
# usage Timespan.to_words (Time.now + 10)
# usage t_one = Time.now
# usage t_two = Time.now + 100
# usage Timespan.to_words t_one
# usage Timespan.to_words t_one..t_two
class Timespan
def self.to_words(time)
unless (time.is_a?(Time) or (time.is_a?(Range) and (time.first.is_a?(Time) and time.last.is_a?(Time))) )
raise 'Time should be a Time/(Range in Time) object #{time.class} given'
@gouthamvel
gouthamvel / number_series_helper.rb
Created September 29, 2011 10:18
function to generate sequence from number like 992345x8xx, x being replaced by 0..9
def generate_sequence(str)
list = [str.downcase]
while (num = list.find{|s| s.index 'x'})
list = list + get_list(list.delete num)
end
list
end
def get_list(str)
loc_x = str.index 'x'
@gouthamvel
gouthamvel / cmd_exe.rb
Created October 11, 2011 06:12
command execution using open3, with a post execution block
def cmd_exe(cmd, &block)
output = nil
log_to_scribe("JR-ncpr-upload"," executing command #{cmd}")
Open3.popen3(cmd) do |stdin, stdout, stderr|
output = stdout.readlines
error = stderr.readlines
unless error.empty?
p "#{cmd} Failed "
log_to_scribe("JR-ncpr-upload","#{cmd} Failed, with error #{error.to_s}")
yield false if block_given?
@gouthamvel
gouthamvel / open3_example.rb
Created October 13, 2011 10:12
open3 sample
require 'open3'
def cmd_exe(cmd, &block)
output = nil
Open3.popen3(cmd) do |stdin, stdout, stderr|
output = stdout.readlines
error = stderr.readlines
p error
unless error.empty?
p "#{cmd} Failed "
yield false if block_given?
@gouthamvel
gouthamvel / helper.rb
Created October 28, 2011 09:09
recursively_symbolize_keys
require 'active_support/core_ext/hash/keys'
class Hash
def recursively_symbolize_keys!
self.symbolize_keys!
self.values.each do |v|
if v.is_a? Hash
v.recursively_symbolize_keys!
elsif v.is_a? Array
v.recursively_symbolize_keys!
require 'eventmachine'
# Code adapted from EM's tests:
# https://github.com/eventmachine/eventmachine/blob/master/tests/test_processes.rb#L43
ls = ""
EM.run {
start_process = Proc.new do |id|
d = EM::DeferrableChildProcess.open( "/bin/bash -c 'slee 1 2> /tmp/err-em.log'" )
d.callback {|*args|
# template for
class ForkedProcess
def initialize *args
end
def receive_data data
#EM.popen form_cmd, ForkedProcess
end
#ruby -v # 1.9.2
#gem list|grep em-ssh # em-ssh(0.1.0)
require 'em-ssh'
# runs the command on server and gives the callback call
def do_ssh(cmd)
do_ssh_var = 'ssh'
EM::Ssh.start('localhost','goutham') do |ssh|
puts 'ssh start'
ssh.exec!(cmd) do |channel, stream, data|
# in /app/models/tag_note.rb
class TagNote < ActiveRecord::Base
belongs_to :tag
end
# in /app/models/acts_as_taggable_on/tag.rb
class ActsAsTaggableOn::Tag
has_one :tag_note
end