Tuesday, July 26, 2011

rspec documentation

Resque Cheatsheet


Status

Resque.info
Resque.queues
Resque.redis
Resque.size(queue_name)

# check out what's coming next in the queue
# Resque.peek(archive_queue)
# Resque.peek(archive_queue, 1, 5)
# Resque.peek(archive_queue, 59, 30)
Resque.peek(queue_name, start=1, count=1)

Workers

Resque.workers
Resque.working
Resque.remove_worker(worker_id) # find worker_id from one of the above methods

Queue Management

# For testing a worker, I usually call the 'perform' method directly.
# Resque.enqueue(ArchiveWorker)
# Resque.enqueue(ArchiveWorker, 'matching', 'arguments')
Resque.enqueue(klass, *args)
Resque.dequeue(klass, *args)
Resque.remove_queue(queue_name)

Callbacks

# Each of these can either take a block, or be assigned to with a Proc
Resque.before_first_fork(&blk)
Resque.before_fork(&blk)
Resque.after_fork(&blk)

Problems

Redis connects to wrong host - Redis connects to localhost:6379 by default. Customize this by doing the following:
Resque.redis = 'hostname:port:db'  # all 3 values are optional
Workers die stop after first batch completes - This is caused by the workers losing their connection to MySQL. See this gist for a fix and an explanation. Alternatively, you can add this line at the beginning of your 'perform' method:
ActiveRecord::Base.reconnect!

Rake Tasks with Parallel Prerequisites


Tasks with Parallel Prerequisites

Rake allows parallel execution of prerequisites using the following syntax:
multitask :copy_files => [:copy_src, :copy_doc, :copy_bin] do
puts "All Copies Complete"
end
In this example, copy_files is a normal rake task. Its actions are executed whereever all of its prerequisites are done. The big difference is that the prerequisites (copy_src,copy_bin and copy_doc) are executed in parallel. Each of the prerequisites are run in their own Ruby thread, possibly allowing faster overall runtime.


warning: already initialized constant ruby


I have few modules named "Source" and in all modules i have same constants and i keep getting this warning message.below is how i fix this

Module Source
  Source.constants.each do |c|
         remove_const(c.to_sym)
  end  
  #########################
  declare all your constants here
  #########################
end

Faster CSV open and read


FCSV.open('test.csv') do |csv|
csv.each do |row|
puts row.first
break if csv.lineno >= 10
end
end
http://www.ruby-forum.com/topic/160439

Monday, July 18, 2011

Working with Files in Ruby

Read input from console in Ruby


puts "Enter A"
a
= gets.chomp
puts
"Enter B"
b
= gets.chomp
c
= Integer(a) + Integer(b)
puts c
you can also pass the parameters through the command line. Command line arguments are stores in the array ARGV. so ARGV[0] is the first number and ARGV[1] the second number
#!/usr/bin/ruby

first_number
= ARGV[0].to_i
second_number
= ARGV[1].to_i

puts first_number
+ second_number
and you call it like this
% ./plus.rb 5 6
==> 11