Class: NEAR::Balance
- Inherits:
-
Object
- Object
- NEAR::Balance
- Defined in:
- lib/near/balance.rb
Overview
Represents a NEAR balance.
Constant Summary collapse
- ZERO =
The canonical zero balance.
self.new(0).freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(quantity) ⇒ Balance
constructor
A new instance of Balance.
-
#inspect ⇒ String
The balance as a Ⓝ-prefixed string.
-
#to_f ⇒ Float
The balance as a floating-point number.
-
#to_i ⇒ Integer
The balance as an integer.
-
#to_r ⇒ Rational
The balance as a rational number.
-
#to_s ⇒ String
The balance as a string.
Constructor Details
#initialize(quantity) ⇒ Balance
Returns a new instance of Balance.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/near/balance.rb', line 19 def initialize(quantity) @quantity = case quantity when BigDecimal then quantity when Rational then quantity when Integer then quantity when Float then quantity when String then BigDecimal(quantity) else raise ArgumentError, "invalid quantity: #{quantity.inspect}" end end |
Class Method Details
.from_near(s) ⇒ Object
12 13 14 |
# File 'lib/near/balance.rb', line 12 def self.from_near(s) self.new(s) end |
.parse(s) ⇒ Object
8 9 10 |
# File 'lib/near/balance.rb', line 8 def self.parse(s) self.new(s.to_f / 10**24) end |
Instance Method Details
#inspect ⇒ String
The balance as a Ⓝ-prefixed string.
38 39 40 |
# File 'lib/near/balance.rb', line 38 def inspect "Ⓝ #{@quantity.to_f}" end |
#to_f ⇒ Float
The balance as a floating-point number.
69 |
# File 'lib/near/balance.rb', line 69 def to_f; @quantity.to_f; end |
#to_i ⇒ Integer
The balance as an integer.
63 |
# File 'lib/near/balance.rb', line 63 def to_i; @quantity.to_i; end |
#to_r ⇒ Rational
The balance as a rational number.
57 |
# File 'lib/near/balance.rb', line 57 def to_r; @quantity.to_r; end |
#to_s ⇒ String
The balance as a string.
46 47 48 49 50 51 |
# File 'lib/near/balance.rb', line 46 def to_s case @quantity when BigDecimal then @quantity.to_s('F') else @quantity.to_s end end |