class BTC

Constants

BIT
BTC
MBTC
MIN_BIT
MIN_BTC
MIN_MBTC
MIN_SATOSHI
SATOSHI

Public Class Methods

from_bits(amt) click to toggle source
# File lib/btc.rb, line 24
def self.from_bits(amt)
  raise ArgumentError, "bits amount must not be less than #{MIN_BIT}" unless self.valid_bits?(amt)
  self.new(BigDecimal.new(amt.to_f.to_s) * BIT)
end
from_mbtc(amt) click to toggle source
# File lib/btc.rb, line 19
def self.from_mbtc(amt)
  raise ArgumentError, "mBTC amount must not be less than #{MIN_MBTC}" unless self.valid_mbtc?(amt)
  self.new(BigDecimal.new(amt.to_f.to_s) * MBTC)
end
from_satoshis(amt) click to toggle source
# File lib/btc.rb, line 29
def self.from_satoshis(amt)
  raise ArgumentError, 'Satoshi amount must be > 1 or 0' unless self.valid_satoshis?(amt)
  self.new(BigDecimal.new(amt.to_i.to_f.to_s) * SATOSHI)
end
new(amt) click to toggle source
# File lib/btc.rb, line 14
def initialize(amt)
  raise ArgumentError, "BTC amount must not be less than #{MIN_BTC}" unless self.class.valid_btc?(amt)
  @amt = BigDecimal.new(amt.to_f.to_s)
end
valid_bits?(amt) click to toggle source
# File lib/btc.rb, line 42
def self.valid_bits?(amt)
  amt.to_f >= MIN_BIT || amt.to_f == 0.0
end
valid_btc?(amt) click to toggle source
# File lib/btc.rb, line 34
def self.valid_btc?(amt)
  amt.to_f >= SATOSHI.to_f || amt.to_f == 0.0
end
valid_mbtc?(amt) click to toggle source
# File lib/btc.rb, line 38
def self.valid_mbtc?(amt)
  amt.to_f >= MIN_MBTC || amt.to_f == 0.0
end
valid_satoshis?(amt) click to toggle source
# File lib/btc.rb, line 46
def self.valid_satoshis?(amt)
  amt.to_i >= 0
end

Public Instance Methods

*(other) click to toggle source
# File lib/btc.rb, line 86
def *(other)
  self.class.new(@amt * other.to_btc)
end
**(exp) click to toggle source
# File lib/btc.rb, line 90
def **(exp)
  self.class.new(@amt ** exp)
end
+(other) click to toggle source
# File lib/btc.rb, line 74
def +(other)
  self.class.new(@amt + other.to_btc)
end
-(other) click to toggle source
# File lib/btc.rb, line 82
def -(other)
  self.class.new(@amt - other.to_btc)
end
/(other) click to toggle source
# File lib/btc.rb, line 78
def /(other)
  self.class.new(@amt / other.to_btc)
end
==(other) click to toggle source
# File lib/btc.rb, line 94
def ==(other)
  @amt == other.to_btc
end
inspect() click to toggle source
# File lib/btc.rb, line 98
def inspect
  "#<BTC:#{to_btc.to_f} mBTC:#{to_mbtc.to_f} bits:#{to_bits.to_f} Satoshis:#{to_satoshis.to_f}>"
end
to_bits() click to toggle source
# File lib/btc.rb, line 58
def to_bits
  @amt / BIT
end
to_btc() click to toggle source
# File lib/btc.rb, line 50
def to_btc
  @amt
end
to_f() click to toggle source
# File lib/btc.rb, line 66
def to_f
  @amt.to_f
end
to_i() click to toggle source
# File lib/btc.rb, line 70
def to_i
  @amt.to_i
end
to_mbtc() click to toggle source
# File lib/btc.rb, line 54
def to_mbtc
  @amt / MBTC
end
to_satoshis() click to toggle source
# File lib/btc.rb, line 62
def to_satoshis
  @amt / SATOSHI
end