Skip to content

Commit 38411ca

Browse files
nobumatzbot
authored andcommitted
[ruby/resolv] Fix centimeters representation in Resolv::LOC::Size#to_s
`Size` smaller than 1m was represented as rational. ```ruby Resolv::LOC::Size.create("0.01m").to_s #=> "1/100m" ``` Fix to represent in the proper form for `create`. ruby/resolv@2ff9e0585d
1 parent b59ae16 commit 38411ca

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

lib/resolv.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,8 +3305,8 @@ def initialize(scalar)
33053305
attr_reader :scalar
33063306

33073307
def to_s # :nodoc:
3308-
s = @scalar.unpack("H2").join.to_s
3309-
return ((s[0].to_i)*(10**(s[1].to_i-2))).to_s << "m"
3308+
s, = @scalar.unpack("C")
3309+
return "#{(s >> 4) * (10.0 ** ((s & 0xf) - 2))}m"
33103310
end
33113311

33123312
def inspect # :nodoc:

test/resolv/test_resource.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def test_size_create
4545
private def assert_size(input, base, power)
4646
size = Resolv::LOC::Size.create(input)
4747
assert_equal([(base << 4) + power], size.scalar.unpack("C"))
48+
assert_equal(size, Resolv::LOC::Size.create(size.to_s))
4849
end
4950

5051
def test_coord

0 commit comments

Comments
 (0)