Monday, May 30, 2011

A Ruby interface for gathering process information on your operating system

Ruby task schedular

Exit from rufus-scheduler ruby

you can use "scheduler.stop" to stop the rufus/scheduler execution.
 require 'rubygems'
require 'rufus/scheduler'

scheduler = Rufus::Scheduler.start_new

scheduler.in '1m' do
puts "hhhhhhhhhhhaaaaaaaaaaaaiiiiiiiiiiiiiiii"
if (some condition)
scheduler.stop
end
end

scheduler.join

Parse media:content tag using Nokogiri

I need to extract the URL from this tag:



"media:content url="http://video.ted.com/talk/podcast/2011/None/MikeMatas_2011.mp4" fileSize="15533795" type="video/mp4" />"


doc = Nokogiri::XML(open("http://www.ted.com/talks/rss"))
doc.css('item').each do |item|
   puts item.at_xpath('media:content')['url']
end

Thursday, May 26, 2011

Protect Your Web Server From Spambots

Grouping array ruby

Create arrays inside an array.



require 'enumerator'

>> a = (0..12).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>> b = a.enum_for(:each_slice,4).to_a
=> [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12]]
below is how to iteration
>> b.each do |c|
?> c.each do |value|
?> puts value
>> end
>> end

Redis and Resque example application ruby