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 2 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
I like to parse data like:
data = [{"dev_type"=>[{"rate"=>1000, "class_type"=>"2020"}], "sys_type"=>{"techs"=>[{"tech"=>"2", "type"=>"classA"}]}, "MOD"=>[{"id"=>"00:00:00:08:00:00", "add"=> "0"}], "user"=>"test", "host_id"=>1, "SYSTEM"=>2}]
for instance if i like to extract "rate" in dev_type.
data.each do |d|
pp d["dev_type"][0]["rate"]
end
# >> 1000
same to extract for type in sys_type i need:
data.each do |d|
pp d["sys_type"]["techs"][0]["type"]
end
# >> ClassA
But this i think not the right way doing for extracting data, what i wanted to do is to extract values like "type" in sys_type, "rate" in dev_type, "id" and "add" in MOD , and "user" . What is the best way of doing this.
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 7 years ago.
Improve this question
If I have
footnotes = { "apple" => "is a fruit", "cat" => "is an animal", "car" => "a transport"}
How can I get the index of these?, something like:
footnotes["cat"].index
# => 1
There's no need to first retrieve the keys:
footnotes.find_index { |k,_| k== 'cat' } #=> 1
As to the question of whether hash key-value pairs have an index (since v1.9), I would just point out that the Ruby monks decided to supply us with Enumerable#find_index, which of course means Hash.instance_methods.include?(:find_index) #=> true.
You can do
footnotes.to_a.index {|key,| key == 'cat'}
# => 1
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 am grouping array items using the below code but it feels slow.
Is there a faster/better way to group?
#tasks_sorted = #tasks.group_by(&:start_date).map do |month, data|
hash = {"date" => month}
data.each {|placement| hash["tasks"] = data}
hash
end
I think below you are looking for :
#tasks_sorted = #tasks.group_by(&:start_date).map do |month, data|
{"date" => month, "tasks" => data }
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
r is an array that has three items that is the result of
r = Part.components.products.uniq
where Part HABTM Component and Component HABTM Product.
Why does this code:
class Array
def p_object_ids
puts each { object_id }.join(", ")
end
end
p r.class
r.p_object_ids
p r.count
generate this output:
Array
#<User:0x00000006535650>, #<User:0x000000065338f0>, #<User:0x000000065336e8>
1
It returns 1 when it's not really an array of three items, but an Array containing a single ActiveRecord. The correct implementation of what I was looking for turned out to be:
class Part
def products
Prooduct.joins(components: :part).where(parts: {id: self.id})
end
end
and not
self.components.map(&:products).uniq.to_a
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 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}