Access each key in array of hashes [on hold]












-1














I have the following array of hashes.



[{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]


I want to loop over each media_url like this:



Array.each do |media|
media.media_url
end


But I get the following error:



undefined method `media_url' for Hash:0x00007fb987684d48









share|improve this question















put on hold as unclear what you're asking by sawa, snakecharmerb, Tomasz Mularczyk, arghtype, SiKing Dec 27 at 20:23


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1




    Cannot be reproduced. There is no method Array.each.
    – sawa
    Dec 27 at 16:39


















-1














I have the following array of hashes.



[{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]


I want to loop over each media_url like this:



Array.each do |media|
media.media_url
end


But I get the following error:



undefined method `media_url' for Hash:0x00007fb987684d48









share|improve this question















put on hold as unclear what you're asking by sawa, snakecharmerb, Tomasz Mularczyk, arghtype, SiKing Dec 27 at 20:23


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1




    Cannot be reproduced. There is no method Array.each.
    – sawa
    Dec 27 at 16:39
















-1












-1








-1







I have the following array of hashes.



[{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]


I want to loop over each media_url like this:



Array.each do |media|
media.media_url
end


But I get the following error:



undefined method `media_url' for Hash:0x00007fb987684d48









share|improve this question















I have the following array of hashes.



[{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]


I want to loop over each media_url like this:



Array.each do |media|
media.media_url
end


But I get the following error:



undefined method `media_url' for Hash:0x00007fb987684d48






ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 at 16:40









sawa

129k27197299




129k27197299










asked Dec 27 at 14:20









arnaudjnn

22




22




put on hold as unclear what you're asking by sawa, snakecharmerb, Tomasz Mularczyk, arghtype, SiKing Dec 27 at 20:23


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






put on hold as unclear what you're asking by sawa, snakecharmerb, Tomasz Mularczyk, arghtype, SiKing Dec 27 at 20:23


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    Cannot be reproduced. There is no method Array.each.
    – sawa
    Dec 27 at 16:39
















  • 1




    Cannot be reproduced. There is no method Array.each.
    – sawa
    Dec 27 at 16:39










1




1




Cannot be reproduced. There is no method Array.each.
– sawa
Dec 27 at 16:39






Cannot be reproduced. There is no method Array.each.
– sawa
Dec 27 at 16:39














2 Answers
2






active

oldest

votes


















4














Use to access hash values - it's not JS :)



a = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]
a.each {|h| h['media_url'] }





share|improve this answer





























    2














    Alternatively, you could refer to the values of specific key, by using .fetch() method on the Hash.



    arr = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]

    arr.each {|h| h.fetch('media_url') }


    in case key has not been found in some of the hashes, can specify default value:



    arr.each {|h| h.fetch('media_url') { 'https://default_url.jpg' } } 


    To directly return an output as an Array of links, you can simply use .map() instead:



    arr.map {|h| h.fetch('media_url') { nil } } 





    share|improve this answer




























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Use to access hash values - it's not JS :)



      a = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]
      a.each {|h| h['media_url'] }





      share|improve this answer


























        4














        Use to access hash values - it's not JS :)



        a = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]
        a.each {|h| h['media_url'] }





        share|improve this answer
























          4












          4








          4






          Use to access hash values - it's not JS :)



          a = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]
          a.each {|h| h['media_url'] }





          share|improve this answer












          Use to access hash values - it's not JS :)



          a = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]
          a.each {|h| h['media_url'] }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 27 at 14:27









          mrzasa

          8,307103878




          8,307103878

























              2














              Alternatively, you could refer to the values of specific key, by using .fetch() method on the Hash.



              arr = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]

              arr.each {|h| h.fetch('media_url') }


              in case key has not been found in some of the hashes, can specify default value:



              arr.each {|h| h.fetch('media_url') { 'https://default_url.jpg' } } 


              To directly return an output as an Array of links, you can simply use .map() instead:



              arr.map {|h| h.fetch('media_url') { nil } } 





              share|improve this answer


























                2














                Alternatively, you could refer to the values of specific key, by using .fetch() method on the Hash.



                arr = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]

                arr.each {|h| h.fetch('media_url') }


                in case key has not been found in some of the hashes, can specify default value:



                arr.each {|h| h.fetch('media_url') { 'https://default_url.jpg' } } 


                To directly return an output as an Array of links, you can simply use .map() instead:



                arr.map {|h| h.fetch('media_url') { nil } } 





                share|improve this answer
























                  2












                  2








                  2






                  Alternatively, you could refer to the values of specific key, by using .fetch() method on the Hash.



                  arr = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]

                  arr.each {|h| h.fetch('media_url') }


                  in case key has not been found in some of the hashes, can specify default value:



                  arr.each {|h| h.fetch('media_url') { 'https://default_url.jpg' } } 


                  To directly return an output as an Array of links, you can simply use .map() instead:



                  arr.map {|h| h.fetch('media_url') { nil } } 





                  share|improve this answer












                  Alternatively, you could refer to the values of specific key, by using .fetch() method on the Hash.



                  arr = [{"comments_count"=>3, "like_count"=>341, "media_type"=>"IMAGE", "media_url"=>"https://url1.jpg", "permalink"=>"https://www.url.com", "timestamp"=>"2018-09-16T11:29:09+0000", "id"=>"17881817992270180"}, {"comments_count"=>1, "like_count"=>209, "media_type"=>"IMAGE", "media_url"=>"https://url2.jpg", "permalink"=>"https://www.url2.com", "timestamp"=>"2018-09-15T18:38:59+0000", "id"=>"17950602214183642"}]

                  arr.each {|h| h.fetch('media_url') }


                  in case key has not been found in some of the hashes, can specify default value:



                  arr.each {|h| h.fetch('media_url') { 'https://default_url.jpg' } } 


                  To directly return an output as an Array of links, you can simply use .map() instead:



                  arr.map {|h| h.fetch('media_url') { nil } } 






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 27 at 19:57









                  amark

                  312




                  312















                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas