From: Rob Biedenharn Date: 2009-02-19T01:59:03+09:00 Subject: Re: Hash acting weird with scRUBYt On Feb 18, 2009, at 8:21 AM, Rick DeNatale wrote: > On Tue, Feb 17, 2009 at 7:58 PM, Patrick L. wrote: > >> Hi Rob, >> So I tried what you suggested. >> >> Here's the new code: http://pastie.org/392399 >> And here's the new error: http://pastie.org/392397 >> > > Some comments. > > First the line > url_hash = {} > is useless. I get the impression that you are coming from some > statically > typed language and think of this as a declaration of the type of the > variable url_hash. Ruby doesn't have typed variables, and neither > has a > mechanism for or a need for declaring types. > > Ok, that said,if you inspect what the next line is actually returning: > > url_hash = istock_data.to_hash > p url_hash > > You should see something like: > > [{:url=>"/file_closeup/nature/nature-backgrounds/8546665-foggy-lane- > with-trees.php?id=8546665"}, .... > {}, {}, {}, {}, {}, {}, {}] > > Which is an array of hashes. The fetch call is looking for a string > containing the url to be fetched. > > So, instead you need to take another approach, and > 1) extract the url from each hash, > 2) Scrape each individual url, and collect the results. > > Something like: > > http://pastie.org/392796 > > This doesn't work because the urls are relative, and I don't have > the time > or motivation to figure that out for you, but this should get you > another > step along the way. > > -- > Rick DeNatale > > Blog: http://talklikeaduck.denhaven2.com/ > Twitter: http://twitter.com/RickDeNatale (Note that I've not looked at your new pastie since Rick has.) Some hints: URI.join with a full URI and a path (absolute or relative) will give a URI back. irb> require 'uri' => true irb> URI.join("http://example.com/", ?> "/file_closeup/nature/nature-backgrounds/8546665- foggy-lane-with-trees.php?id=8546665") => # irb> puts _ http://example.com/file_closeup/nature/nature-backgrounds/8546665-foggy-lane-with-trees.php?id=8546665 => nil And Rick's step 1 gets the hint: array_of_url_values_as_strings = \ array_of_hashes_with_url_keys.map {|h| h[:url]} -Rob P.S. Because you've shown your attempted code, you are getting more help than someone who just posts a plea for help. Thanks for that. (and I hope others learn this lesson, too) Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com