开发者

String Comparison in ruby with Sqlite3 db's

What I'm trying to do here is comparing 2 strings within the sqlite dbs and I want to say I'm a bit stumped. The basic goal of the program is that the user makes a request for a site to be crawled. The crawler then breaks the site down into jobs or links. I'm trying to avoid duplicate crawls and requests by saying

if(request is already in jo开发者_运维百科bs list)
    {
    do nothing
    }
else
    {
    Anemone.crawl(....)

Anemone is the web spider framework for ruby by the way.

So the comparison in the if statement is as follows.

    if(@allRequests.first.url.to_s.eql?@allJobs.first.url.to_s)

        puts 'TEST TEST TEST'
        puts 'Request is already detected in job list'

@allJobs and @allRequests are both just select * from Jobs.Requests

@allJobs = Job.all
@allRequests = Request.all

The comparison seems to be failing and it will create duplicates no problem. Has anyone any ideas?


On a side note. How does one delete a field from the .all variables.

allJobs.drop "where url = myurl.com" ?


So this was an adventure. For the sake of someone else hitting the same problem. follow these instructions.

If you have an object, do a YAML dump

puts YAML::dump(@requestToUpdate)
puts YAML::dump(@jobToCompare)

And make sure its not nil. This was the first big clue. requestToUpdate was fully populated while jobToCompare was not.

After a bit of sloothing around I found that there were better ways to specify what those 2 objects were rather than

@Object.first.url

replace with:

@requestToUpdate = Request.find_by_url(@usersRequestedSite)
@jobToCompare = Job.find_by_url("http://"+@usersRequestedSite+"/")

url is just a string param of my objects and can be swapped for anything else. The added http and / are for the differences between them strings to match.

Afterwards it was just a matter of comparing the strings in the same way

else if (("http://"+@requestToUpdate.url+"/").eql? @jobToCompare.url)

Voila. I had my first string comparison. I'll never forget my first time. :£

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜