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
My data is in the form of:
a = [
{
"a_id":101,
"a_value":100000.0,
"a_quantity":360.0
},
{
"a_id":108,
"a_value":110000.0,
"a_quantity":210.0
},
{
"a_id":104,
"a_value":105000.0,
"a_quantity":310.0
}
]
I would like the data to be sorted in descending order of a_value. I have tried:
a.sort_by {|k| k[:a_value] }.reverse
But it does not get sorted.
What you have works. Just don't forget to assign the sorted collection to a variable (sort_by and reverse do not change the collection).
Bonus: here's arguably a nicer version (one pass, instead of two)
a.sort_by{ |v| -v[:a_value] }
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 8 years ago.
Improve this question
My current array of object "sum" is:
[{"sum":{"key1":0,"key2":"2014","key3":0,"key4":"8","key5":0,"key6":"0","key7":0}},
{"sum":{"key1":0,"key2":"2014","key3":0,"key4":"12","key5":0,"key6":"1","key7":0}}]
The target is:
[{"key1":0,"key2":"2014","key3":0,"key4":"8","key5":0,"key6":"0","key7":0,
{"key1":0,"key2":"2014","key3":0,"key4":"12","key5":0,"key6":"1","key7":0}]
Use Array#flat_map
array.flat_map(&:values)
# is same as :
array.flat_map { |hash| hash.values }
# or simply if you have
array.map { |hash| hash["sum"] }
I used symbolize_keys what #Muistooshort mentioned in this topic How to convert Object to array of hashes with symbol keys
So, this is code what I used:
result = arr.map{|e| e.attributes.symbolize_keys}
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
I use the excellent faker gem to generate random words for my models. Eg. product.name = Faker::Lorem.word
Sometimes I need to generate a sentence, and I want the length of the sentence to
vary each time.
How to achieve this with ruby?
How about:
result = rand(max_size).times.map { produce_word }
Since you have not provided enough information, this is my approach, [*1..100].sample will return a random number between 1 and 100, so looping that times the string which is returned bya method named get_word will get stored in the array word_array
word_array = []
[*1..100].sample.times do
word_array << get_word
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
Need to add two arrays before that need to add some value at starting of the first array. Look at the following:
#conunty_format = [ "country", "imps", "revenue","network_revenue"]
final_ca = [2000,55.62,88.69]
I need to add "Canada" to final_ca and generate hash with corresponding county_format.
Hash[#conunty_format.zip(final_ca.unshift('canada'))]
=> {"country"=>"canada", "imps"=>2000, "revenue"=>55.62, "network_revenue"=>88.69}
You can use Array Zip and some properties of Array to achieve it in a single line. see the below code.
resulted_hash = #country_format.zip(final_ca.unshift("Canada")).inject({}) do |r, s| r.merge!({s[0] => s[1]}) end
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have this:
array = ["a","b","c"]
How do I get this:
"a","b","c"
I need to get the items out of the array, each double-quoted, separated by a comma.
array.collect { |a| "\"#{a}\"" }.join(",")
I'm just started to learn ruby, I guess:
return ["a","b","c"].map{|i| '"' + i + '"'}.join(",")
Might you want to get this:
irb(main):009:0> [1, 2, 3].map(&:to_s).join('","')
=> "1\",\"2\",\"3"
"a","b","c" this is not abject (these are 3 objects). But in ruby any code returns object value. So you should know what you want to get: 1 object (I returning string in this example) or various. If you want to get 3 objects you should extract array like this:
a, b, c = [1,2,3].map(&:to_s)
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 11 years ago.
Improve this question
Here is my code sample, let me know if it can be further improved?
excludedb = if File.exist?(arg)
IO.read(arg).split(',').map { |db_name| db_name.strip }.delete_if { |db_name| db_name == "" }
else
["master", "model", "sybsystemdb", "sybsystemprocs", "tempdb", "sybsecurity", "pubs2", "pubs3", "dbccdb", "sybmgmtdb"]
end
Here's a couple of tiny improvements.
You can replace
.map { |db_name| db_name.strip }
with
.map(&:strip)
And also you can use string array literal
%w{master model sybsystemdb}