How to add values in nested hash in ruby's way - ruby-on-rails

Here is a nested hash. We want to add all the value of "subtotal" together in ruby's way. Please be noted that the key of "0" and "1342119042142" could be any other unknown strings (number of keys is at least one. Could be more than one) when performing the addition.
{"0"=>{"lease_item_id"=>"3",
"subtotal"=>"100"},
"1342119042142"=>{"lease_item_id"=>"1",
"subtotal"=>"100",
"_destroy"=>"false"}}}
Thanks.

Like this:
set up hash:
s = {"0"=>{"lease_item_id"=>"3", "subtotal"=>"100"},
"1342119042142"=>{"lease_item_id"=>"1", "subtotal"=>"100","_destroy"=>"false"}}
calculate total:
total = s.inject(0) { |i, j| i + j.last["subtotal"].to_i}
Explanation: Look here for documentation. Basically inject is given an initial value (in the above code it is 0) and it passes the given value to the given block, where it gets set to what is returned from the block in each iteration. So in the above code, initially it is 0, on the first iteration it is set to 0 + 100 and now is equal to 100, and on the second [and final] iteration it is set to 100 + 100, 200.

Assuming h is your hash and the subtotal can be decimal value:
h.values.sum{|x| x['subtotal'].to_f}

hash = {"0"=>{"lease_item_id"=>"3", "subtotal"=>"100"}, "1342119042142"=>{"lease_item_id"=>"1", "subtotal"=>"100", "_destroy"=>"false"}}
sum = hash.values.reduce(0){|sum,inner| sum + inner["subtotal"].to_i }

Related

Is there a way to check if a hash value in Ruby is the same throughout or compare only the values for equality?

I have the below code that returns the number of instances of an item in an array as a hash. I now need to check if the value is the same. For example if the hash is like this = {1=>3, 2=>3} i need to check if the value is the same, in this case it is but dont know how to check this.
arr.inject(Hash.new(0)) {|number,index| number[index] += 1 ;number}
Thanks
So, given h = { 1 => 3, 2 => 3 }, if I got you, you want to know if the values are ALL the same. If you knew the keys you could do
all_the_same = h[1] == h[2]
If there are more keys you want to check
all_the_same = h.values_at(1, 2, 3, 4).uniq.length == 1
If you don't know how many keys you have or which are these keys you could do
all_the_same = h.values.uniq.length == 1

How to compute each cell of a line in an array with Ruby on Rails 5.2?

While importing from an excel file to a database, I need to format a hierarchy so it appears with leading zeros:
10.1.1.4 must be transformed into 1.010.001.001.004
I tried to iterate through and concatenate the elements:
record.hierarchy = spreadsheet.cell(i,2).split('.').each do |t|
index = index || '1.'
index = index + '.' + (((t.to_i + 1000).to_s).last(3))
end
which actually returns and array of ["10", "1", "1", "4"], not computed. I would expect this to return the last evaluated value: index
I tried to compute it directly inside the array:
record.hierarchy = '1.' + (((spreadsheet.cell(i,2).split('.').each).to_i + 1000).to_s).last(3).join('.')
which raises an undefined method to_i for enumerator.
Can someone explain me how to structure and solve this computation?
Thanks
Use #rjust.
'10.1.1.4'.split('.').map { |l| l.rjust(3, '0') }.join('.')
Your first solution uses assignment with #each. #each will not return modified array.
It is not necessary to convert the string to an array, modify the elements of the array and then join the array back into a string. The string can be modified directly using String#gsub.
str = '10.1.1.4'
('1.' + str).gsub(/(?<=\.)\d+/) { |s| sprintf("%03d", s.to_i) }
#=> "1.010.001.001.004"
See Kernel#sprintf.
(?<=\.) is positive lookbehind that requires the matched digits to be preceded by a period. I've assumed the string is known to contain between one and three digits before and after each period.
You can try different function for leading zeroes and inject to not set default value inside the loop
record.hierarchy = spreadsheet.cell(i,2).split('.').inject('1') do |result, t|
result + '.' + t.rjust(3, '0')
end

I need explanation of method inject

I am trying to figure out how inject method works
Can someone explain this
def mysort
if djeca.any?
djeca.order(name: :asc).inject([]) { |sum, c| sum += c.mysort}.uniq
else
[self]
end
mysort is method of model class Books
In controller I call method mysort :
#mybooks= Books.find_by(name: 'renesansa')
#mybookss= #mybooks.leaf_wms
djeca.order(name: :asc).inject([]) { |sum, c| sum += c.mysort}.uniq
is equivalent to
sum = []
djeca.order(name: :asc).each{|c| sum = sum + c.mysort}
sum.uniq
Adding arrays is actually concatening, so your code just appends all the c.mysort into an array.
If I understand it correctly, you could also write :
djeca.order(name: :asc).map{|c| c.mysort}.flatten.uniq
Depending on your audience, you might want to write one or the other.
Note that you don't need to assign a value to sum in the inject block, it is done automatically.
(1..10).inject(0){|mem,i| mem += i}
#=> 55
(1..10).inject(0){|mem,i| mem + i}
#=> 55
You should follow this link https://samurails.com/tips/ruby-ruby-on-rails-inject/
For example:
result = [1,2,3,4].inject(0) { |sum, number| sum + number }
Here, the process starts from 0 index to the 3 index. First inject adds 1 & 2 to get the sum of two values and stored in sum variable(i.e. sum= 3) and then it takes the sum value and adds 3 to it, to get result(i.e sum=6) and so on. Finally you will get the result like 10.
djeca.order(name: :asc)
Retrieves array of active records
.inject([])
Looping through each active record. The initial output of inject is empty array [].
{ |sum, c| }
c - each active record
sum - Inject output value. Initially its []. On each iteration the value is appended to the array. (sum += [some values])

Is there a way to apply multiple method by one liner in ruby?

There is an array like this:
a = [1,2,3,4]
I want to get the return values of size and sum like this.
size = a.size
sum = a.sum
Is there a way to get both values by a one-liner like this?
size, sum = a.some_method(&:size, &:sum)
In Ruby, you can do multiple assignments in one line:
size, sum = a.size, a.sum
It doesn't make it more readable, though.
You could do this:
a = [1,2,3,4]
methods = [:size, :max, :min, :first, :last]
methods.map { |m| a.send m }
#=> [4, 4, 1, 1, 4]
Another possible solution:
size, sum = a.size, a.reduce { |a,b| a = a + b }
Previous answers are correct, but if OP was actually concerned about walking the array multiple times, then array.size does not walk the array, it merely returns the length, thus there is no saving from a oneliner in that regard.
On the other hand, if size was just an example and the question is more about making multiple operations on an array in one go, then try something like this:
arr = [1,2,3,4,5,6]
product,sum = arr.inject([1,0]){|sums,el| [sums[0]*el, sums[1]+el]}
# => [720, 21]
That is, inject the array with multiple initial values for the results and then calculate new value for every element.

Nested tables and numerical keys in Lua

I'm not sure if this is possible due to the numerical indices, but hopefully someone can point me in the right direction.
Given the table of:
t = { 13, 200, 12, 15, 23 }
how can I nest a table using the numbers?
t["200"] = {"stuff", "more stuff", "even more stuff"}
doesn't seem to work, as it'll create a position 200 and fill in the empty cells with null. I'd add a letter as a suffix/prefix, but the problem comes trying to sort the table numerically. Is this even possible, or am I stuck with a different method? Thanks!
Slight edit due to a realisation:
t["200"] = {"stuff", "more stuff", "even more stuff"}
actually creates a key of "200", whereas:
t[200] = {"stuff", "more stuff", "even more stuff"}
creates the index 200 with everything else null.
First, DeadMG is correct; you used a string rather than a numerical index. However, even if you did use a number index, it wouldn't help.
If you do this:
someTable = {"value1", "value2", {"value3a", "value3b"}};
someTable[50] = {"value50a", "value50b"};
The length of the table, #someTable, will still be 3. Why? Because Lua defines arrays in a table based on contiguous elements. Remember: you can access any element of any table; they are all conceptually filled with nil until you give them an actual value.
Lua defines length for a table as the number of values in a table if you start counting from numerical index 1 until you reach the first nil value. Since someTable[4] is nil, the length is 3.
If you want to insert a new element at the end of an array table, then you can do this:
someTable[#someTable + 1] = "newValue";
The value can itself be a table:
someTable[#someTable + 1] = {"newValuea", "newValueb"};
If you're just asking how to access a nested table, that's simple, and it has nothing to do with the keys you use.
There is nothing special about nested tables. Tables are values, and table entries can be any value, including other tables.
If you have a table, and want to walk the array entries in it, you use this:
local aTable = {"first", "second", "third", ...}
for i, value in ipairs(aTable) do
--`value` contains the entries in the table.
end
A nested table is no different; it is simply a matter of getting the table.
local nestedTable = { "first", "second", "third", ...}
nestedTable[#nestedTable + 1] = {"newFirst", "newSecond", ...}
local aTable = nestedTable[#nestedTable];
for i, value in ipairs(aTable) do
--`value` contains the entries in the table.
end
Or you could just do ipairs(nestedTable[#nestedTable]). Note that the particular key used here (an integer value) is entirely unimportant. That key could have been a string, a floating-point number, another table, some user-data, etc. It doesn't matter.
Note also that we use ipairs because we only want to iterate over the array members of the table. The length of the array is defined above. If we wanted to loop over every member of the table, we would use pairs instead of ipairs. Of course, pairs does an unordered search, so it is not guaranteed to be in array order.
If you want to recursively find every element in a nested table, you can do this:
local function RecursiveSearch(aTable)
for key, value in pairs(aTable) do --unordered search
if(type(value) == "table") then
RecursiveSearch(value)
else
--Do something with this.
end
end
end
Note that the above can do an infinite loop, since it is possible for a table to have circular references:
local tableA = {}
local tableB = {tableA}
local tableA[1] = tableB
RecursiveSearch(tableA) --Infinite loop.
Perhaps it helps to view your assignment like this:
t = { [1] = 13, [2] = 200, [3] = 12, [4] = 15, [5] = 23 }
To change what is currently 200 (namely t[2]), you do:
t[2] = {"stuff", "more stuff", "even more stuff"}
Edit: that results in your table looking like this:
t = { [1] = 13, [2] = {"stuff", "more stuff", "even more stuff"}, [3] = 12, [4] = 15, [5] = 23 }
-- or, equivalent::
t = { 13, {"stuff", "more stuff", "even more stuff"}, 12, 15, 23 }
The trouble is your use of "". Your table t contains a bunch of numbers, and you're entering a string as the key. You want to iterate over the table and do... something that you didn't particularly well define. However, you can't add to a table whilst iterating over it, so you might have to do some funny stuff.
t = { 13, 200, 12, 15, 23 }
newt = {};
for key, value in pairs(t) {
newt[value] = { };
}
This will create a table entry in newt, where the key is a value in the table t, for all values in t.

Resources