Skip to content

Instantly share code, notes, and snippets.

@sydneyitguy
sydneyitguy / eth.rb
Created January 8, 2020 16:42
Ethereum address validation and normalization in Ruby
# Extracted from https://github.com/se3000/ruby-eth
#
# Dependencies:
# - gem 'digest-sha3'
# - gem 'rlp'
#
# Usage:
# - Eth::Utils.valid_address?('0x4Db7569F90bd836294B11c8b08B853d2de499Ced')
# => true
# - Eth::Utils.format_address('0x4db7569f90bd836294b11c8b08b853d2de499ced')
class String
def to_bool
return true if self == true || self =~ (/^(true|t|yes|y|1|1\.0)$/i)
return false if self == false || self.blank? || self =~ (/^(false|f|no|n|0)$/i)
raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
end
end
class Fixnum
def to_bool