Access each key in array of hashes [on hold]
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
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.
add a comment |
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
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 methodArray.each
.
– sawa
Dec 27 at 16:39
add a comment |
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
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
ruby
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 methodArray.each
.
– sawa
Dec 27 at 16:39
add a comment |
1
Cannot be reproduced. There is no methodArray.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
add a comment |
2 Answers
2
active
oldest
votes
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'] }
add a comment |
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 } }
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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'] }
add a comment |
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'] }
add a comment |
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'] }
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'] }
answered Dec 27 at 14:27
mrzasa
8,307103878
8,307103878
add a comment |
add a comment |
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 } }
add a comment |
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 } }
add a comment |
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 } }
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 } }
answered Dec 27 at 19:57
amark
312
312
add a comment |
add a comment |
1
Cannot be reproduced. There is no method
Array.each
.– sawa
Dec 27 at 16:39