Hash acting weird with scRUBYt


#1

Hey folks,
This hash of mine keeps throwing an “undefined method `each_value’”
error and I cant figure out why…it’s the strangest thing. Here, take a
look:

Thanks in advance!


#2

On Feb 17, 2009, at 7:19 PM, Patrick L. wrote:

Hey folks,
This hash of mine keeps throwing an “undefined method `each_value’”
error and I cant figure out why…it’s the strangest thing. Here,
take a
look:

Thanks in advance!

Perhaps you just meant to:
urls.values.each do |url|
But it’s whining about an Array which seems to be at odds with the
istock_data.to_hash which one would suspect gives you a Hash

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#3

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


#4

On Feb 18, 2009, at 8:21 AM, Rick DeNatale wrote:

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"},

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”)
=> #<URI::HTTP:0x1c8e82
URL: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 B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#5

On Tue, Feb 17, 2009 at 7:58 PM, Patrick L. removed_email_address@domain.invalid 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"},
{:url=>"/file_closeup/celebrations-holidays/holiday-symbols/8549349-st-patrick-s-day-beer-bottle-caps-set.php?id=8549349"},
{:url=>"/file_closeup/celebrations-holidays/8542775-fire-campfire.php?id=8542775"},
{:url=>"/file_closeup/technology/8546657-asphalt-cutter.php?id=8546657"},
{:url=>"/file_closeup/food-and-drink/fruits-and-vegetables/8550674-vegetables-illustration.php?id=8550674"},
{:url=>"/file_closeup/animals/insects/8550593-sunflower.php?id=8550593"},
{:url=>"/file_closeup/health-and-beauty/body/8546651-measuring-the-waist.php?id=8546651"},
{:url=>"/file_closeup/nature/flowers/8522299-red-rose.php?id=8522299"},
{:url=>"/file_closeup/actions/8575484-marching-feet.php?id=8575484"},
{:url=>"/file_closeup/food-and-drink/cooking/8550489-cheese.php?id=8550489"},
{:url=>"/file_closeup/technology/electronics/8542843-laser-dvd.php?id=8542843"},
{:url=>"/file_closeup/industry/8575477-drywall-background.php?id=8575477"},
{:url=>"/file_closeup/isolated-objects/isolated-background-objects/8550212-stationary-vector.php?id=8550212"},
{:url=>"/file_closeup/people/8545005-cute-little-girl.php?id=8545005"},
{:url=>"/file_closeup/nature/8575473-repetitive-beauty.php?id=8575473"},
{:url=>"/file_closeup/illustrations-vectors/vector-icons/8551189-occupations-icons.php?id=8551189"},
{:url=>"/file_closeup/nature/landscapes/8546633-white-gazebo-in-park.php?id=8546633"},
{:url=>"/file_closeup/locations-and-travel/8546629-hot-springs-of-yellowstone.php?id=8546629"},
{:url=>"/file_closeup/locations-and-travel/travel-backgrounds/8546625-tyrolian-meadow.php?id=8546625"},
{:url=>"/file_closeup/food-and-drink/8545105-golden-nuts.php?id=8545105"},
{:url=>"/file_closeup/nature/gardens/8546623-grass-c-sped.php?id=8546623"},
{:url=>"/file_closeup/food-and-drink/8522290-herring.php?id=8522290"},
{:url=>"/file_closeup/business/business-concepts/8546619-coins.php?id=8546619"},
{:url=>"/file_closeup/celebrations-holidays/holiday-symbols/8568117-love.php?id=8568117"},
{:url=>"/file_closeup/architecture-and-buildings/churches/8546617-chapel.php?id=8546617"},
{:url=>"/file_closeup/celebrations-holidays/valentine-s-day/8568114-love-note.php?id=8568114"},
{:url=>"/file_closeup/nature/flowers/8543896-white-daisies-with-a-cloudy-sky.php?id=8543896"},
{:url=>"/file_closeup/architecture-and-buildings/architectural-detail/8561525-corner-detail.php?id=8561525"},
{:url=>"/file_closeup/nature/8544844-smoke-tree.php?id=8544844"},
{:url=>"/file_closeup/nature/bodies-of-water/8547420-suspension-bridge.php?id=8547420"},
{}, {}, {}, {}, {}, {}, {}]

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


#6

On Wed, Feb 18, 2009 at 11:59 AM, Rob B.
<removed_email_address@domain.invalid

wrote:

Some comments.
[{:url=>"/file_closeup/nature/nature-backgrounds/8546665-foggy-lane-with-trees.php?id=8546665"},
2) Scrape each individual url, and collect the results.

Something like:

http://pastie.org/392796

And Rick’s step 1 gets the hint:
array_of_url_values_as_strings =
array_of_hashes_with_url_keys.map {|h| h[:url]}

Which is more the way I’d write this code myself than my pastie example.
I’ve got a tendency to write more verbose, step by stuff code when I’m
trying to help someone in order to get the point across, or in some
cases
when I’m thinking a problem through myself. If I do this last, I
typically
refactor the code to make it tighter as another step.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale