Set precision for all big decimals in Ruby on Rails - ruby-on-rails

Is there any way of defining a precision for all the BigDecimal numbers in Ruby or Ruby on Rails?
I setted the precision using the limit method, but that didn't seem to work out:
irb(main):003:0> BigDecimal.limit
=> 25
irb(main):004:0> num = '0.' + 0.to_s * 30 + '1'
=> "0.0000000000000000000000000000001"
irb(main):005:0> decimal = BigDecimal(num)
=> #<BigDecimal:9614780,'0.1E-30',9(45)>
irb(main):006:0> puts decimal.to_s
0.0000000000000000000000000000001
=> nil
irb(main):007:0> BigDecimal.limit
=> 25
Did I misunderstand the usage of the limit method? Is there any other that can achieve what I want?
The app uses Ruby 2.3.4 and Rails 4.2.8
Disclaimer: I already know how to truncate and how to only set the precision for individual values. I really need a way to set this "globally" (I mean, for all the new Big Decimals I instantiate or manipulate).

You can try
2.1.7 :056 > num = '0.' + 0.to_s * 30 + '1'
=> "0.0000000000000000000000000000001"
2.1.7 :057 > decimal = BigDecimal(num)
=> #<BigDecimal:7be2178,'0.1E-30',9(45)>
2.1.7 :058 > puts decimal.to_s
0.0000000000000000000000000000001
=> nil
2.1.7 :059 > '%.2f' % decimal
=> "0.00"

Related

truncate text after exceeding certain size

Rails offers the truncate method when you want to truncate text that exceeds a certain character length. This is the example provided here:
truncate("Once upon a time in a world far far away")
# => "Once upon a time in a world..."
It truncates a given text after a given :length if text is longer than :length
But I have the following example:
str = "abcdefhijklmno"
After 12 characters I want it truncated, so the above text should look like this:
abcdefhijklm...
But I tried using the truncate method and cannot get the desired result:
> str = "abcdefhijklmno"
=> "abcdefhijklmno"
> str.truncate(15)
=> "abcdefhijklmno"
> str.truncate(14)
=> "abcdefhijklmno"
> str.truncate(13)
=> "abcdefhijk..."
> str.truncate(12)
=> "abcdefhij..."
I would think that truncate(12) would do it but it truncates after 9 characters. What am I doing wrong?
argument of truncate means size of output string (with "..."):
str.truncate(13)
=> "abcdefhijk..."
str.truncate(13).size
=> 13
You can change default omission(...) by empty space if you want:
str.truncate(13, omission: '')
=> "abcdefhijklmn"
More about Rails String#truncate here.
I don't believe str.truncate will do what you want out of the box. But since it's really just an extra if statement, it's easy to write:
def trunc(str, length)
addition = str.length > length ? '...' : ''
"#{str.truncate(length, omission: '')}#{addition}"
end
Or slightly simplified, and without a Rails dependency (as mentioned by Ilya in the comments):
def trunc(str, len)
"#{str.first(len)}#{'...' if str.size > len}"
end
And the test cases:
2.2.1 :005 > s = 'abcdefghijklmno'
=> "abcdefghijklmno"
2.2.1 :006 > trunc(s, 20)
=> "abcdefghijklmno"
2.2.1 :007 > trunc(s, 15)
=> "abcdefghijklmno"
2.2.1 :008 > trunc(s, 14)
=> "abcdefghijklmn..."
2.2.1 :009 > trunc(s, 13)
=> "abcdefghijklm..."
2.2.1 :010 > trunc(s, 0)
=> "..."
2.2.1 :011 > trunc(s, 1)
=> "a..."

Sorting number string in ruby

I have two numbers of different length:
"103" and "11"
In irb:
2.1.1 :005 > "11" > "103"
=> true
2.1.1 :006 > "11" < "103"
=> false
Why does this happen? I understand I can to a .to_i for each string, but if this is a rails query where the column type is string, anything I can do about this?
Strings are sorted lexicographically which means that "1" comes after "0", and "103" comes before "11", and before "1122344", and before "1abc".
You cannot compare strings as if they were numbers, you need to parse them as numbers before you can do that.
The only way I can think of, is to make sure they are padded with enough zeroes before they are turned into a string: "000103", "000011"...
Strings are being compared character by character. Hence '11' > '103' execution stops on a second character and returns true, since '1'.chr > '0'.chr
They are strings. And they are compared by String#ord value.
So '1'.ord # => 49 and '0'.ord # => 48. That is why
'11' > '10' # => true
# and
'11' > '100' # => true
as well as
'b' > 'a' # => true
'a'.ord # => 97
'b'.ord # => 98
# and
'b' > 'aaaaa' # => still true

rails 3.2 BigDecimal and floating point numbers comparison returns absurd result

I'm having problems working with BigDecimal in Rails 3.2.3 and ruby 1.9.3p362
the following sequence of number comparisons output absurd results
1.9.3-p362 :060 > b = BigDecimal.new('620.56')
=> #<BigDecimal:68665e0,'0.62056E3',18(18)>
1.9.3-p362 :061 > b <= 620.56
=> false
1.9.3-p362 :062 > b > 620.56
=> true
1.9.3-p362 :063 > (b - 620.56) > 0
=> false
1.9.3-p362 :064 > (b - 620.56) == 0
=> true
in other words, this is telling that:
B > A
and
B - A == 0
what am I missing ?
Welcome to the wonderful world of Floating Point Arithmetic.
This website gives a good introduction of how and why (check the Basic Answers first), but what it comes down to is:
Computer are not very accurate when it comes to floating point numbers. Rounding errors are everywhere, so you have to be careful what you do.

Ruby pack - TypeError: can't convert String into Integer

I am getting TypeError: can't convert String into Integer, I found duplicate answer too, but I am facing this error for 'pack'.
Other confusion is, it is working fine with ruby 1.8.7, not with ruby 1.9.3, here is the code, I am using jruby1.7.2
irb(main):003:0> length=nil
=> nil
irb(main):004:0> token_string ||= ["A".."Z","a".."z","0".."9"].collect { |r| r.to_a }.join + %q(!$:*)
=> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$:*"
irb(main):005:0> token = (0..(length ? length : 60)).collect { token_string[rand( token_string.size)]}.pack("c*")
TypeError: can't convert String into Integer
from (irb):5:in `pack'
from (irb):5
from /home/appandya/.rvm/rubies/ruby-1.9.3-p374/bin/irb:13:in `<main>'
irb(main):006:0>
Any Idea?
string[x] in Ruby 1.8 gives you a Fixnum (the character code) and in 1.9 it gives you a single character string.
Array#pack turns an array into a binary sequence. The "c*" template to pack converts an array of Ruby integers into a stream of 8-bit signed words.
Here are the solutions, those comes from the Google Groups
1.
Background
>> %w(a b c).pack('*c')
TypeError: can't convert String into Integer
from (irb):1:in `pack'
from (irb):1
from /usr/bin/irb:12:in `<main>'
>> [1, 2, 3].pack('*c')
=> "\x01"
>> %w(a b c).map(&:ord).pack('*c')
=> "a"
Solution
irb(main):001:0> length=nil
=> nil
irb(main):002:0> token_string ||= ["A".."Z","a".."z","0".."9"].collect { |r| r.to_a }.join + %q(!$:*)
=> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$:*"
irb(main):003:0> (0..(length ? length : 60)).collect { token_string[rand( token_string.size)]}.map(&:ord).pack("c*")
=> "rd!:!LcxU3ON57*t2s520v*zvvdflSNAgU6uq14SiD00VUDlm9:4:tJz5Ri5o"
irb(main):004:0>
2.
The return type of String's [] function was Fixnum in 1.8 but is String in 1.9:
>JRUBY_OPTS=--1.9 ruby -e "puts 'a'[0].class"
String
>JRUBY_OPTS=--1.8 ruby -e "puts 'a'[0].class"
Fixnum
JRuby 1.7.x defaults to acting like Ruby 1.9.3. You need to set JRUBY_OPTS.
3.
try join instead of pack
irb(main):004:0> length=nil
=> nil
irb(main):005:0> token_string ||= ["A".."Z","a".."z","0".."9"].collect { |r| r.to_a }.join + %q(!$:*)
=> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$:*"
irb(main):006:0> token = (0..(length ? length : 60)).collect { token_string[rand( token_string.size)]}.join("c*")
=> "Fc*Dc*1c*6c*ac*Kc*Tc*Qc*Hc*jc*Ec*Kc*kc*zc*sc*3c*ic*hc*kc*wc**c*Wc*$c*Kc*Ic*Uc*Cc*bc*Pc*1c*!c*mc*Bc*lc*dc*ic*Dc*sc*Ac*Bc*nc*Kc*mc*Lc*oc*Zc*Xc*jc*6c*2c*Uc*ec*Yc*Dc*vc*Ic*Uc*5c*Zc*3c*o"
irb(main):007:0>
4.
if you're just trying to make a string of random characters that are 8-bit clean you may want to look at Random#bytes and something like Base64.encode64.
5.
active_support/secure_random also has a nice API for these things
I would recommend you take a look at: Array Method Pack, essentially the .pack("c") expects the elements of the array to be integers. You could try .pack("a*")
Use token = (0..(length ? length : 60)).collect { token_string[rand( token_string.size)]}.pack("a"*61)
You get the same result. I am using 61 because, going from 0..60 has 61 elements.

ActiveRecord object marked as dirty when old value and new value are both numeric zero

I have a model called Day that represents a day in a timesheet. I've noticed that whenever I call #day.save it's writing to the database, even though none of the object's properties have had their values changed.
#day = Day.last
=> #<Day lunch_minutes: 0, updated_at: "2012-08-19 12:09:40", work_hours: 5.5>
A day has its length in hours, and the length of its lunch break in minutes, stored. I've cropped out some properties that aren't relevant.
#day.lunch_minutes
=> 0
#day.lunch_minutes = 0
=> 0
#day.changes
=> {"lunch_minutes"=>[0, 0]}
#day.lunch_minutes_changed?
=> true
That should be false. Compare to a value that isn't zero:
#day.work_hours = 5.5
=> 5.5
#day.work_hours_changed?
=> false
So if I call save, this gets called. Ideally there would be no unnecessary database interaction here.
#day.save
(0.5ms) UPDATE "days" SET "lunch_minutes" = 0, "updated_at" = '2012-08-19 12:22:59.586860' WHERE "days"."id" = 48
I'm not sure if this is a Rails bug or if I'm doing some incorrectly somewhere. It looks like it could be an issue in "changes_from_zero_to_string?" - I think adding a && value != 0 to that method would fix it - but I want to know if anyone else has seen this/a fix for this before?
What version of rails are you using? I just had a go in my app (3.1.5/1.8.7) and it doesn't behave this way.. I just used a random integer property on one of my models to test with:
1.8.7 :006 > o = Order.first
=> <Order id:...>
1.8.7 :007 > o.order_items_count
=> 0
1.8.7 :008 > o.order_items_count = 0
=> 0
1.8.7 :009 > o.changes
=> {}
1.8.7 :010 > o.order_items_count = '0'
=> "0"
1.8.7 :011 > o.changes
=> {}
1.8.7 :012 > o.save
(0.1ms) BEGIN
(0.1ms) COMMIT
=> true
It appears to be a bug.
Interestingly, according to the code, if you do:
#day.lunch_minutes = '0'
It will probably think it has not changed!
Try that, and if indeed this change causes #day.lunch_minutes_changed? to be false, then make sure it is reported as an issue to https://github.com/rails/rails.

Resources