Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Hash interpreted as an Array

I'm using a hash to store a little data needed to parse a website including URL and the specific instructions to use Nokogiri to parse the headlines of the site. However the nested Hash I used is interpreted as an Array.

webSite = {  :everdoH =>  
                        { :url => "http://www.everardoherrera.com/" ,
                          :instruc=> Proc.new PageToParse.css('.contentheading').css('.contentpagetitle')} 
           }

If I do:

puts webSite.class
webSite.each {|aSite| puts aSite.class }

The I get this:

>> Hash
>> Array

Therefore I can't use any of the Hash indexing properties.

Any idea why I am not getting class type Hash on the nested part?

like image 436
André Hess Avatar asked Feb 25 '26 01:02

André Hess


1 Answers

Because Hash#each, Hash#each_pair passes key-value pair (array) as parameters.

webSite = {  :everdoH =>
  { :url => "http://www.everardoherrera.com/" ,
    :instruc=> 'blah'
  }
}
webSite.each { |aSite| p aSite }
# => [:everdoH, {:url=>"http://www.everardoherrera.com/", :instruc=>"blah"}]

Try one of followings (printing class of value, not key-value pair):

webSite.each {|aSite| puts aSite[1].class }
# => Hash
webSite.each {|key, aSite| puts aSite.class }
# => Hash
webSite.each_value {|aSite| puts aSite.class }
# => Hash
like image 167
falsetru Avatar answered Feb 26 '26 18:02

falsetru