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}
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 months ago.
Improve this question
[Trying to count from 1-10 using a for loop][1]
For our purposes here, pay attention to the second for loop in this picture.
[1]: https://i.stack.imgur.com/iUnGD.png
Use this code for print result in console:
public static void LoopSolution()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"Count: {i + 1}");
}
}
In console result:
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 1 year ago.
Improve this question
I'm looking a way to check string contains an uppercase. The following code:
word := "Hello"
for _,v := range word{
if .... {
fmt.Print("contain uppercase letter")
}
}
You have to check each rune:
hasUpper:=false
for _,r:=range word {
if unicode.IsUpper(r) {
hasUpper=true
break
}
}
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] }
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
Im aiming for a regex formula to return chunks of a string based on a character, if this string contains L1 then its going to be only one chunk, if L2 is found it would return 2 chunks, L3 = 3 chunks.
Example
Lets assume we have this string
"L2N1N1"
and we would like to get 2 string
"L2N1" and "L2N1N1"
Another example
"L3N1N1N2"
to return 3 strings
"L3N1" "L3N1N1" "L3N1N1N2"
Im using Ruby
"L3N1N1N2".sub(/L(\d)(?:N\d)+/) do |m|
$1.to_i.times.map { |i| m[0..3+2*i] }.join(' ')
end
#⇒ "L3N1 L3N1N1 L3N1N1N2"
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}