Class: Bytes
- Inherits:
-
Object
- Object
- Bytes
- Defined in:
- lib/bytes.rb,
lib/bytes/version.rb
Constant Summary collapse
Class Method Summary collapse
- .banner ⇒ Object
- .convert(*args) ⇒ Object
- .from_hex(hexstr) ⇒ Object
- .new(*args) ⇒ Object
- .root ⇒ Object
- .to_hex(str) ⇒ Object
- .version ⇒ Object
Class Method Details
.banner ⇒ Object
16 17 18 |
# File 'lib/bytes/version.rb', line 16 def self. "bytes/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" end |
.convert(*args) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bytes.rb', line 31 def self.convert( *args ) ## used by Bytes() in global Kernel converter method if args.size == 1 if args[0].is_a? Array ## assume array of bytes ## to be done else ## assume String ## todo/fix: use coerce to_str if arg is NOT a string - why? why not? str = args[0] ## if str.encoding == Encoding::ASCII_8BIT ## assume it's binary data - use as is (no hexstring conversion) new( str ) ## todo/check: return str as-is (without new) - why? why not? else ## assume it's a hexstring from_hex( str ) end end else ## todo/fix: throw argument error end end |
.from_hex(hexstr) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/bytes.rb', line 16 def self.from_hex( hexstr ) if ['0x', '0X'].include?( hexstr[0...2] ) [hexstr[2..-1]].pack('H*') ## cut-of leading 0x or 0X if present else [hexstr].pack('H*') end end |
.new(*args) ⇒ Object
12 13 14 |
# File 'lib/bytes.rb', line 12 def self.new( *args ) String.new( *args ).b end |
.root ⇒ Object
20 21 22 |
# File 'lib/bytes/version.rb', line 20 def self.root "#{File.( File.dirname(File.dirname(File.dirname(__FILE__))) )}" end |
.to_hex(str) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/bytes.rb', line 24 def self.to_hex( str ) # note: unpack returns string with <Encoding:US-ASCII> # conver to default encoding ## todo/fix: do NOT hardcode UTF-8 - use default encoding - how? str.unpack('H*').first.encode("UTF-8") end |
.version ⇒ Object
12 13 14 |
# File 'lib/bytes/version.rb', line 12 def self.version VERSION end |