Archive

Archive for the ‘ruby’ Category

Multiple ruby files work together

October 29th, 2009 No comments

When writing the first few Ruby programs, there is a tend to place all the code in a single file. But as time passes by and the Ruby programs grow, it is nature that at some point we have to break our code up into logical groupings and place each group in a separate file or files.

following is an examle:

in foo.rb:
puts “it is foo”
$foo = 3 # $ for global variable

in bar.rb:
puts “it is bar”
$bar = 3 # $ for global variable

in test.rb:
require ‘foo’ # pay attention –no “.rb”
load ‘bar.rb’ # pay attention — “.rb” is there
test = 1 + $foo + $bar

when execute test.rb, it shows :
it is foo
it is bar
9

Categories: ruby Tags:

The ruby code to find which weekday is current day

June 10th, 2009 No comments

At first, I failed to find which weekday is today by following codes:

timetoday = Time.now

if timetoday =~ /Fri/
puts “Friday”
end

I found in the internet that following codes works:

timetoday = ” ”
timetoday = Time.now.to_s()

if timetoday =~ /Fri/
puts “Friday”
end

Categories: ruby Tags:

watir installation error 14001

March 17th, 2009 Comments off

I encountered a error 14001 when I tried to install watir 1.6.2 in a new computor, I googled and found:

the version of win32-api should be 1.3.0 or later
the version of windows-api should be 0.3.0 or later
the version of windows-pr should be 0.9.9 or later

After fix the above version problems, the watir can work properly.

Categories: ruby, watir Tags:

Hpricot vs Mechanize vs ScrAPI vs Watir vs ScRUBYt!

November 1st, 2008 No comments

Hpricot vs Mechanize vs ScrAPI vs Watir vs ScRUBYt!

For autoposting and scraping, at first I got familiar with watir, and then I learned somthing about Hpricot, now I am concentrated on Mechanize.

I think all of the tools stated above are quite good.

Related Blogs

Categories: ruby Tags:

if statement with multiple conditions in ruby

October 7th, 2008 No comments

It is difficult to find the examples demonstrating how to combine two conditions in the if statement of ruby, I tried and found following code is ok:

tmp = rand(10)

if ((tmp > 5) && (ie77.text.include? ‘fine’))
puts “fine”

else

sleep 7
end

Categories: ruby Tags:

firebug + hpricot

October 1st, 2008 No comments

Following code is trying to scrape the content in a webpage, the content cannot be picked by the scraping software tool I used:
require ‘rubygems’
require ‘hpricot’
require ‘open-uri’

url = “http://homemsg.focus.cn/msgview/607/50129006.html”
doc = Hpricot(open(url))
td_contents = (doc/”/html/body/table[8]/tbody/tr/td[4]/table[2]/tbody/tr/td”).inner_html
puts td_contents

It did not work, there must be something wrong.

by firebug, I can copy the xpath(/html/body/table[8]/tbody/tr/td[4]/table[2]/tbody/tr/td), and copy its innerHTML.

Categories: ruby Tags: ,