I'm working with an ApplicationHelper method that translates Time objects into 'humanized' time measurements in my view:
def humanize_seconds s
if s.nil?
return ""
end
if s > 0
m = (s / 60).floor
s = s % 60
h = (m / 60).floor
m = m % 60
d = (h / 24).floor
h = h % 24
w = (d / 7).floor
d = d % 7
y = (w / 52).floor
w = w % 52
output = pluralize(s, "second") if (s > 0)
output = pluralize(m, "minute") + ", " + pluralize(s, "second") if (m > 0)
output = pluralize(h, "hour") + ", " + pluralize(m, "minute") if (h > 0)
output = pluralize(d, "day") + ", " + pluralize(h, "hour") if (d > 0)
output = pluralize(w, "week") + ", " + pluralize(d, "day") if (w > 0)
output = pluralize(y, "years") + ", " + pluralize(w, "week") if (y > 0)
return output
else
return pluralize(s, "second")
end
end
It's working great, but I'm running into an issue when translating the end result of a method designed to list time intervals in a specified location:
RFIDTag.rb:
def time_since_first_tag_use
product_selections.none? ? "N/A" : Time.now - product_selections.order(staged_at: :asc).first.staged_at
end
Product.rb:
def first_staged_tag
rfid_tags.map { |rfid| rfid.time_since_first_tag_use.to_i }.join(", ")
end
View: (html.erb):
Putting the value out there works, and lists the values as first_staged_tag is meant to do, but it only does so in seconds:
<% #products.order(created_at: :desc).each do |product| %>
<td><%= product.name %></td> #Single product name
<td><%= product.first_staged_tag %></td> list, i.e. #40110596, 40110596, 39680413, 39680324
<%end%>
While converting in the usual way <td><%= humanize_seconds(product.first_staged_tag) %></td>, as has worked for single values gives this error:
comparison of String with 0 failed
Extracted source (around line #88):
86 return ""
87 end
88 if s > 0
89 m = (s / 60).floor
90 s = s % 60
91 h = (m / 60).floor
Meanwhile, trying to apply that method in the Product model first_staged_tag method generates a NoMethod error on humanize_seconds. How can I get my list of times to recognize the time conversion?
An iteration of everything tried is in the comments.
Solved! The tags had to be mapped in the Product model, and converted there:
#Product.rb
def first_staged
rfid_tags.map { |rfid| rfid.time_since_first_tag_use.to_i }
end
Then iterated over again in the view as a whole:
<%= product.first_staged.map {|time| humanize_seconds(time) }.join(", ") %>
Related
I can get this data with the following code. But it runs too slow:
local handle = io.popen("exiftool image.webp")
local result = handle:read("*a")
handle:close()
Is there a more elegant way to get the metadata?
UPD:
I use this software:
docker (20.10.7)
openresty/openresty:xenial (1.15.8.3)
luarocks (3.2.1)
LuaJIT (2.1.0-beta3)
Here is an example of a picture with a UserComment field: link
Exiftool sees this property:
$ exiftool -EXIF:UserComment Johnrogershousemay2020.webp
User Comment : {"foo":"bar"}
This method is less elegant, but it does not require to run external application :-)
function get_webp_user_comment(file_name)
local file = io.open(file_name, "rb")
local exif_offset, exif_found, is_big_endian, user_comment = 12
local function read_string(offset, size)
file:seek("set", offset)
return file:read(size)
end
local function read_uint(offset, size)
local n, s = 0, read_string(offset, size)
for j = 1, size do
n = n * 256 + s:byte(is_big_endian and j or size + 1 - j)
end
return n
end
local function read_uint32(disp)
return read_uint(exif_offset + disp, 4)
end
local function read_uint16(disp)
return read_uint(exif_offset + disp, 2)
end
local function search_for_tag(ifd_disp, tag)
if ifd_disp ~= 0 then
local entry_disp = ifd_disp + 2
for j = 1, read_uint16(ifd_disp) do
if read_uint16(entry_disp) == tag then
return read_uint32(entry_disp + 8), read_uint32(entry_disp + 4)
end
entry_disp = entry_disp + 12
end
return search_for_tag(read_uint32(entry_disp), tag)
end
end
if read_string(0, 4) == "RIFF" and read_string(8, 4) == "WEBP" then
local max_offset = read_uint32(-8)
while exif_offset < max_offset do
local section_name = read_string(exif_offset, 4)
exif_offset = exif_offset + 8
if section_name == "EXIF" then
local endianness = read_string(exif_offset, 2)
is_big_endian = endianness == "MM"
exif_found = is_big_endian or endianness == "II"
if exif_found then
break
end
end
exif_offset = exif_offset + read_uint32(-4)
exif_offset = exif_offset + exif_offset % 2
end
end
if exif_found then
local exif_ifd = search_for_tag(read_uint32(4), 0x8769)
if exif_ifd then
local disp, count = search_for_tag(exif_ifd, 0x9286)
user_comment = read_string(exif_offset + disp + 8, count - 8)
end
end
file:close()
return user_comment
end
Usage example:
local file_name = "path/to/Johnrogershousemay2020.webp"
print(get_webp_user_comment(file_name))
If all you need is 'UserComment', then pass that as a parameter during your popen call:
local handle = io.popen("exiftool -EXIF:UserComment image.webp")
I'm looking for a method that will return this statement in a JSON format.
def statement
total = 0
bonus points = 0
result = 'Car rental for #{#name.to_s}\n'
for r in #rentals
this_amount = 0
case r.car.style
when Car::SUV
this_amount += r.days_rented * 30
when Car::HATCHBACK
this_amount += 15
if r.days_rented > 3
this_amount += (r.days_rented - 3) * 15
end
when Car::SALOON
this_amount += 20
if r.days_rented > 2
this_amount += (r.days_rented - 2) * 15
end
else
end
if this_amount < 0
bonus_points -= 10
end
bonus_points = bonus_points + 1
if r.car.style == Car::SUV && r.days_rented > 1
bonus_points = bonus_points + 1
end
result += r.car.title.to_s + "," + this_amount.to_s + "\n"
total += this_amount
end
result += "Amount owed is " + "#{total.to_s}" + "\n"
result +="Earned bonus points: " + bonus_points.to_s
result
end
What method would I need to add to my class to return this statement in a JSON format? Thank you.
Simplest way to do that
return {:result => result}
at the end of your method.
But if you are using controller to show data to user, I would prefer using .to_json in my controller method
At first, I use this:
math.randomseed(os.time())
This function to check:
function makeString(l)
if l < 1 then return nil end
local s = ""
for i = 0, l do
n = math.random(0, 61)--61
n0 = 0
if n < 10 then n0 = n + 48
elseif n < 36 then n0 = n - 10 + 65
else n0 = n - 36 + 97 end
s = s .. string.char(n0)
end
return s
end
If I use this function:
app:match("/tttt", function()
local ss = ""
for i=1,10 do ss = ss .. makeString(30) .. "\n" end
return ss
end)
I receive good different values.
If I use this:
app:match("/ttt", function()
return makeString(30)
end)
and JavaScript jQuery,
:
$("#button5").click(function(){
var ss = ""
for(var i = 0; i < 10; i++)
{
$("#div1").load("/ttt", function() {
ss = ss + $(this).text()
alert(ss);
});
}
$("#div1").text(ss);
});
I recieve the same random strings every one second.
How to fix it? I tried to create database with different random data but I recieve the same strings!!! This is just example I wrote but filling the database gives the same result!##%%
Any ideas to fix it?
I found that this is "feature" of lua to generate random using seconds. Use this link to read more: http://lua-users.org/wiki/MathLibraryTutorial
But the solution which can help is to user /dev/urandom
The link to describe:
http://lua-users.org/lists/lua-l/2008-05/msg00498.html
In my case example:
local frandom = io.open("/dev/urandom", "rb")
local makeString
makeString = function(l)
local d = frandom:read(4)
math.randomseed(d:byte(1) + (d:byte(2) * 256) + (d:byte(3) * 65536) + (d:byte(4) * 4294967296))
if l < 1 then return nil end
local s = ""
for i = 0, l do
local n = math.random(0, 61)
n0 = 0
if n < 10 then n0 = n + 48
elseif n < 36 then n0 = n - 10 + 65
else n0 = n - 36 + 97 end
s = s .. string.char(n0)
end
return s
end
Forgive my lack of code but I can't quite figure out the best way to achieve the following:
two strings (stored as strings because of the leading 0 - they are phone numbers) :
a = '0123456700'
b = '0123456750'
I am trying to find a way to write them as a range as follows
0123456700 - 750
rather than
0123456700 - 0123456750
which I currently have.
It's not as straightforward as getting the last 3 digits of b since the range can vary and perhaps go up to 4 digits so I'm trying to find the best way of being able to do this.
I'd look up the index of the first unequal pair of characters:
a = '0123456700'
b = '0123456750'
index = a.chars.zip(b.chars).index { |x, y| x != y }
#=> 8
And extract the suffix with:
"#{a} - #{b[index..-1]}" if index
#=> "0123456700 - 50"
Here's a method that returns the range:
def my_range(a, b)
a = a.delete(" ") # remove all spaces from string
b = b.delete(" ")
a, b = b, a if a.to_i > b.to_i # a is always smaller than b
ai, bi = a.to_i, b.to_i
pow = 1
while ai > 1
pow += 1
len = pow if ai % 10 != bi % 10
ai /= 10
bi /= 10
end
a + " - " + b[-len..-1]
end
puts my_range("0123456700", "0123456750") # 0123456700 - 750
puts my_range("0123456669", "0123456675") # 0123456669 - 675
puts my_range("0123400200", "0123500200") # 0123400200 - 3500200
puts my_range("012 345 678", "01 235 0521") # 012345678 - 350521
From my personal library (simplified):
def common_prefix first, second
i = 0
loop{break unless first[i] and second[i] == first[i]; i += 1}
first[0, i]
end
a = "0123456700"
b = "0123456750"
c = "0123457750"
common_prefix(a, b)
# => "01234567"
"#{a} - #{b.sub(common_prefix(a, b), "")}"
# => "0123456700 - 50"
"#{a} - #{c.sub(common_prefix(a, c), "")}"
# => "0123456700 - 7750"
Note. This will work correctly only under the assumption that all strings are right padded with 0 to be the same length.
I am working on a client's site, and I'm writing an amortization schedule calculator in in ruby on rails. For longer loan term calculations, it doesn't seem to be breaking when the balance reaches 0
Here is my code:
def calculate_amortization_results
p = params[:price].to_i
i = params[:rate].to_d
l = params[:term].to_i
j = i/(12*100)
n = l * 12
m = p * (j / (1 - (1 + j) ** (-1 * n)))
#loanAmount = p
#rateAmount = i
#monthlyAmount = m
#amort = []
#interestAmount = 0
while p > 0
line = Hash.new
h = p*j
c = m-h
p = p-c
line["interest"] = h
line["principal"] = c
if p <= 0
line["balance"] = 0
else
line["balance"] = p
end
line["payment"] = h+c
#amort.push(line)
#interestAmount += h
end
end
And here is the view:
- #amort.each_with_index do |a, i|
%li
.m
= i+1
.i
= number_to_currency(a["interest"], :unit => "$")
.p
= number_to_currency(a["principal"], :unit => "$")
.pp
= number_to_currency(a["payment"], :unit => "$")
.b
= number_to_currency(a["balance"], :unit => "$")
What I am seeing is, in place of $0.00 in the final payment balance, it shows "-$-inf", iterates one more loop, then displays $0.00, but shows "-$-inf" for interest. It should loop until p gets to 0, then stop and set the balance as 0, but it isn't. Any idea what I've done wrong?
The calculator is here. It seems to work fine for shorter terms, like 5 years, but longer terms cause the above error.
Edit:
Changing the while loop to n.times do
and then changing the balance view to
= number_to_currency(a["balance"], :unit => "$", :negative_format => "$0.00")
Is a workaround, but i'd like to know why the while loop wouldn't work correctly
in Ruby the default for numerical values is Fixnum ... e.g.:
> 15 / 4
=> 3
You will see weird rounding errors if you try to use Fixnum values and divide them.
To make sure that you use Floats, at least one of the numbers in the calculation needs to be a Float
> 15.0 / 4
=> 3.75
> 15 / 4.0
=> 3.75
You do two comparisons against 0 , which should be OK if you make sure that p is a Float.
As the other answer suggests, you should use "decimal" type in your database to represent currency.
Please try if this will work:
def calculate_amortization_results
p = params[:price].to_f # instead of to_i
i = params[:rate].to_f # <-- what is to_d ? use to_f
l = params[:term].to_i
j = i/(12*100.0) # instead of 100
n = l * 12
m = p * (j / (1 - (1 + j) ** (-1 * n))) # division by zero if i==0 ==> j==0
#loanAmount = p
#rateAmount = i
#monthlyAmount = m
#amort = []
#interestAmount = 0.0 # instead of 0
while p > 0
line = Hash.new
h = p*j
c = m-h
p = p-c
line["interest"] = h
line["principal"] = c
if p <= 0
line["balance"] = 0
else
line["balance"] = p
end
line["payment"] = h+c
#amort.push(line)
#interestAmount += h
end
end
If you see "inf" in your output, you are doing a division by zero somewhere.. better check the logic of your calculation, and guard against division by zero.
according to Wikipedia the formula is:
http://en.wikipedia.org/wiki/Amortization_calculator
to improve rounding errors, it's probably better to re-structure the formula like this:
m = (p * j) / (1 - (1 + j) ** (-1 * n) # these are two divisions! x**-1 == 1/x
which is equal to:
m = (p * j) + (p * j) / ((1 + j) ** n) - 1.0)
which is equal to: (use this one)
q = p * j # this is much larger than 1 , so fewer rounding errors when dividing it by something
m = q + q / ((1 + j) ** n) - 1.0) # only one division
I think it has something to do with the floating point operations precision. It has already been discussed here: Ruby number precision with simple arithmetic and it would be better to use decimal format for financial purposes.
The answer could be computing the numbers in the loop, but with precomputed number of iterations and from the scratch.