# Hash acting weird with scRUBYt

**URL:** <https://www.ruby-forum.com/t/hash-acting-weird-with-scrubyt/160589>\
**Category:** Ruby\
**Created:** [February 18, 2009, 1:19am UTC](https://www.ruby-forum.com/t/hash-acting-weird-with-scrubyt/160589 "2009-02-18T01:19:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [February 18, 2009, 1:19am UTC](https://www.ruby-forum.com/t/hash-acting-weird-with-scrubyt/160589/1 "2009-02-18T01:19:17Z")

</div>

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:

- The code [http://pastie.org/392354](http://pastie.org/392354)
- The error message: [http://pastie.org/392355](http://pastie.org/392355))

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [February 18, 2009, 1:38am UTC](https://www.ruby-forum.com/t/hash-acting-weird-with-scrubyt/160589/2 "2009-02-18T01:38:59Z")

</div>

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:
> 
> - The code [http://pastie.org/392354](http://pastie.org/392354)
> - The error message: [http://pastie.org/392355](http://pastie.org/392355))
> 
> 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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [February 18, 2009, 1:58am UTC](https://www.ruby-forum.com/t/hash-acting-weird-with-scrubyt/160589/3 "2009-02-18T01:58:09Z")

</div>

Hi Rob,  
So I tried what you suggested.

Here’s the new code: [http://pastie.org/392399](http://pastie.org/392399)  
And here’s the new error: [http://pastie.org/392397](http://pastie.org/392397)

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [February 18, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/hash-acting-weird-with-scrubyt/160589/4 "2009-02-18T18:00:02Z")

</div>

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/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](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/](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](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](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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [February 18, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/hash-acting-weird-with-scrubyt/160589/5 "2009-02-18T14:22:09Z")

</div>

On Tue, Feb 17, 2009 at 7:58 PM, Patrick L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Rob,  
> So I tried what you suggested.
> 
> Here’s the new code: [http://pastie.org/392399](http://pastie.org/392399)  
> And here’s the new error: [http://pastie.org/392397](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](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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [February 18, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/hash-acting-weird-with-scrubyt/160589/6 "2009-02-18T18:12:26Z")

</div>

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](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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)
