Subtracting Values in two identical hashes in ruby
I would just do:
hash1 = {:total=>{:gold=>100, :dark=>500}, :defensive=>{:gold=>100, :dark=>500}}
hash2 = {:total=>{:gold=>20, :dark=>200}, :defensive=>{:gold=>20, :dark=>200}}
hash1.merge(hash2) { |_, l, r| l.merge(r) { |_, x, y| x - y } }
#=> {:total=>{:gold=>80, :dark=>300}, :defensive=>{:gold=>80, :dark=>300}}
Subtract two hashes in Ruby
Use the reject
method:
class Hash
def difference(other)
reject do |k,v|
other.has_key? k
end
end
end
To only reject key/value pairs if the values are identical (as per mallanaga's suggestion via a comment on my original answer, which I have deleted):
class Hash
def difference(other)
reject do |k,v|
other.has_key?(k) && other[k] == v
end
end
end
Subtract values in hash
arr = [
{:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16617331511,
:timeCapturedmax=>"February 18, 2018 at 09:27 AM", :time_unixmax=>1518964024},
{:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16617216094,
:timeCapturedmax=>"February 18, 2018 at 09:22 AM", :time_unixmax=>1518963724},
{:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16617202279,
:timeCapturedmax=>"February 18, 2018 at 09:18 AM", :time_unixmax=>1518963485},
{:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16616985649,
:timeCapturedmax=>"February 18, 2018 at 09:12 AM", :time_unixmax=>1518963135},
{:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16616404836,
:timeCapturedmax=>"February 18, 2018 at 09:07 AM", :time_unixmax=>1518962829},
{:device=>"router", :portmax=>"Port 1/5/3:2006.0", :discardmax=>16616368250,
:timeCapturedmax=>"February 18, 2018 at 09:03 AM", :time_unixmax=>1518962583}
]
arr.each_cons(2).map do |g,h|
{ device: g[:device], discard_sub: g[:discardmax]-h[:discardmax] }
end
#=> [{:device=>"router", :discard_sub=>115417},
# {:device=>"router", :discard_sub=>13815},
# {:device=>"router", :discard_sub=>216630},
# {:device=>"router", :discard_sub=>580813},
# {:device=>"router", :discard_sub=>36586}]
Check that a hash includes another hash
In Ruby 2.3.
abc.select{|h| h >= ghi}
Note: This was answered before the OP mentioned the Ruby version.
Prior versions of Ruby:
abc.select{|h| h.merge(ghi) == h}
Subtracting from two columns and having the values add up if the name is present more than once
awk to the rescue
awk 'FNR == NR { # collect the keys and count from first file
ks[$1]++
next
}
FNR != NR && FNR==1 { # when switched to second file, print the counts
print "\t$VAR1={" # in the format
for (k in ks) {
print "\t\t" k "=>" ks[k] ","
}
print "\t};\n"
}
{
print $3,$4,$5 # print the extract from second file
if ($5 in ks) { # and compute the accumulated diffs
ss[$5] += $4 - $3
}
}
END { # finally, print the accumulated diffs
print "\n"
for (s in ss) {
print s, ss[s]
}
}' file1 file2
Divide contents of one hash with contents of another in Rails
You can use ActiveRecord::Calculations#average
like this:
daily_average = monthly_subscriptions_new.group_by_day(:created_at)
.average(:total_spend)
Related Topics
Erroneous "Insecure World Writable Dir Foo in Path" When Running Ruby Script
Check Method Call on Model Using Minitest
What Does "Temps.Each(&:Valid)" Mean in Ruby
How to Run Code After Each Line in Ruby
Preserving Case in Http Headers with Ruby's Net:Http
Async Requests Using Sinatra Streaming API
Interested in What the "<<" Does
Rvm Ruby with Tk Installation (Osx)
Cocoon Add Association, How to Limit Number of Associations
How to Include Blank Field in Select_Tag
Check to See If an Array Is Already Sorted
How to Set a Hook to Run Code at the End of a Ruby Class Definition