Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
[:sha,"string"]
[:node_id,"dsd"]
[:stats,{:total=>122},:additions=>23],:deletions=>72}]
[:files,[{:sha=>"456"},:filename=>"456"],{:sha=>"123"},:filename=>"123"]]
I want to access the content of files sha and stats.
client.commit("healtheintent/hcc_reference_service","3a3c56babbc1c77323d178303fa06f8d11e1133d").each do |key,value|
if("#{key}"=="files")
puts ("#{value}")
end
end
Now the values returns the entire content inside the files
Similarily, How do I access the stats
How to iterate over the hash in general
hash = { a: 1, b: 2, c: 2 }
hash.each do |key, value|
if key == :a
puts "a is #{value}"
else
puts "It is not a, it's #{key}"
end
end
Also you can iterate over the keys only or over the values only using each_key or each_value methods
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
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
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'] }
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 } }
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How does one update the array based on symbols? Like
data = []
string = "Hello"
if( !data.include? string )
count += 1
data.insert(-1, {
label: string,
value: count,
})
else
#logic to change count value if string is encountered again
end
I was thinking of finding the index where the string lies and then delete that to insert another updated values at that index. Is this the right approach?
Just use find to get the match, provided its the only one in the array. You can use select to get multiple matches. After that just update the count
As your example is taken out of context and contains errors, I've taken a liberty to make a more complete example.
data = []
strings = ["Hello", "Bye", "Hello", "Hi"]
strings.each do |string|
hash = data.find{ |h| h[:label] == string }
if hash.nil?
data << {label: string, value: 1}
else
hash[:value] += 1
end
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have array of arrays looking something like this :
arr = [[f,f,f,f,f], [f,f,t,f,f], [f,t,f,t,f]]
and am I outputing it formatted on the console like this:
arr.each {|a| puts a.join.gsub('t','<b></b>').gsub('f','<i></i>')}
and it generates something like this:
<i></i><i></i><i></i><i></i><i></i>
<i></i><i></i><b></b><i></i><i></i>
<i></i><b></b><i></i><b></b><i></i>
but it is only in the output. I am wondering how I can assign it to a string? With the new lines and everything, exactly the way it looks,
a= [["f","f","f","f","f"], ["f","f","t","f","f"], ["f","t","f","t","f"]].map do |arr|
arr.join.gsub(/[ft]/) do |x|
if x =~ /f/
'<i></i>'
elsif x =~ /t/
'<b></b>'
end
end
end.join("\n")
puts a
# >> <i></i><i></i><i></i><i></i><i></i>
# >> <i></i><i></i><b></b><i></i><i></i>
# >> <i></i><b></b><i></i><b></b><i></i>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The next code search if keyword appear in hash values and print yes if so,
but it works well in codeacademy console, but in my Rubymine it give me exception
NoMethodError: undefined method `keys' for nil:NilClass
I've tried to use each_key method but it was the same rusult.
arr = [
{ name: "2222", num:"4444 kod"},
{ name: "3222 kod", num:"43423444"},
{ name: "224422", num:"4442424"}
]
p = "kod"
arr.each do |frelancer|
frelancer.keys.each do |key|
if frelancer[key].split(" ").include? (p)
puts "yes"
esle
puts "no"
end
end
Can you give some advice?)
You have 2 mistakes:
You wrote esle instead of else
You are missing one end clause
Your blocks need end keyword. And else should be spelt correctly.
arr.each do |frelancer|
frelancer.keys.each do |key|
if frelancer[key].split(" ").include? (p)
puts "yes"
else
puts "no"
end
end
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here is my code:
class String
def frequency
chars.each_with_object(Hash.new(0)) do |char, h|
h["#{char.upcase}:"] += 1 if char[/[[:alpha:]]/]
end
end
end
I've tried breaking it down in smaller bit's of code, such as using a .times do loop but I couldn't figure it out
for example:
str = "\*"
h["A:"] = count('a').times do
str
end
Are you trying to do something like:
counts = 'aassssvvvvv'.frequency
counts.each{|key,count| puts key + '*'*count}
# A:**
# S:****
# V:*****
Or if you want to change the key you can do:
counts.each{|key,amount| counts[key] = '*'*amount}