Skip to content

Commit

Permalink
BigDecimal.new is deprecated in Ruby 2.7 (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurotaky authored Mar 13, 2020
1 parent da0acf6 commit 4a8f703
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ethereum/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def to_address(hexstring)

def to_wei(amount, unit = "ether")
return nil if amount.nil?
BigDecimal.new(UNITS[unit.to_sym] * amount, 16).to_s.to_i rescue nil
BigDecimal(UNITS[unit.to_sym] * amount, 16).to_s.to_i rescue nil
end

def from_wei(amount, unit = "ether")
return nil if amount.nil?
(BigDecimal.new(amount, 16) / BigDecimal.new(UNITS[unit.to_sym], 16)).to_s rescue nil
(BigDecimal(amount, 16) / BigDecimal(UNITS[unit.to_sym], 16)).to_s rescue nil
end

def from_address(address)
Expand Down
16 changes: 16 additions & 0 deletions spec/ethereum/formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe Ethereum::Formatter do

let (:formatter) { Ethereum::Formatter.new }

it "#to_wei" do
expect(formatter.to_wei(1)).to eq 1000000000000000000
expect(formatter.to_wei(nil)).to be_nil
end

it "#from_wei" do
expect(formatter.from_wei(1000000000000000000)).to eq "1.0"
expect(formatter.from_wei(nil)).to be_nil
end
end

0 comments on commit 4a8f703

Please sign in to comment.