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
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 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.
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 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}
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
Say I have this method
def foo(bar1, bar2)
## code
end
How could I implement the code so that, when I call foo('hello', 'world'), the foo method accesses a hash, giving:
{
:bar1 => 'hello',
:bar2 => 'world'
}
Is there a Ruby (Rails?) built in method, or how could I write it?
def foo(bar1, bar2)
names = method(__method__).parameters.map{|e| e[1]}
Hash[names.zip(names.map {|name| eval(name)})]
end
Don't do that. It's ugly and evil. Give me the whole context, you're doing something wrong.
def foo(bar1, bar2)
{bar1: bar1, bar2: bar2}
end
If what Sergio mentions was the intention of the question, then
def foo(bar1, bar2)
Hash[method(__method__).parameters.map{|_, k| [k, eval(k.to_s)]}]
end
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How this will work without eval
%w{user_id for_whom_id category_id product_status_id}.each do |f|
code = <<-C
it "should fail validation with no #{f}" do
should_fail_on_validation { |p| p.#{f} = nil }
end
C
eval code
end
?
This should work:
%w{user_id for_whom_id category_id product_status_id}.each do |f|
it "should fail validation with no #{f}" do
should_fail_on_validation { |p| p.public_send "#{f}=", nil }
end
end
It's best to avoid the eval if possible. And in this case, it isn't necessary.