Issue
def main()
myDB=DB.new('myUser','myPass','myDB')
record = myDB.handle().select_one('select count(*) from things')
printf "%10d" % record
end
main() result: 30008428
I want to divide this value by 24 to create batches which will be executed every hour. This is the pretty print result:
[#<BigDecimal:ac2fdf94,'0.30008428E8',8(12)>]
I'm pretty confused on how to achieve that. As I understand it "record" is an array with a BigDecimal object in it. I tried to do record[0].div(24) that didn't work either. record.div(24) also didn't work.
Since I've spent quite a bit of time figuring it out now, I feel like I need to turn to the community for a little bit of help.
Thanks in advance.
Solution
The DBI::Handle#select_one
method (which I imagine is what must be hidden behind your DB
module) returns a single record from the database. Because this one record could potentially contain several fields, it returns the record as an array. In this case there is only a single column - the count - so that count value is in record[0]
.
I guess by "pretty print" you mean the output from p record
, which is debugging output rather than just pretty printing. It is the equivalent of puts record.inspect
. In this case it shows a single object (everything within #<...>
) inside an array (the [...]
).
In the case of BigDecimal
objects, inspect
returns a string giving the class name, BigDecimal; the address in hex, ac2fdf94; the current value as a string, 0.30008428E8; and the current (maximum) number of significant figures, 8 (12).
The short answer to your question is that you probably want to write
count = record[0].to_i
and use count
thereafter, which is a normal Fixnum
, so your division looks like
per_hour = count / 24.0
A value of 30,008,428 is well within the maximum Fixnum
value, which depends on your machine word size but is probably 230 or over one billion. And even if you exceed that value, Ruby will - mostly silently - convert to a Bignum
for you so nothing bad should happen.
Answered By - Borodin Answer Checked By - Clifford M. (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.