Class: Bytes

Inherits:
Object
  • Object
show all
Defined in:
lib/bytes.rb,
lib/bytes/version.rb

Constant Summary collapse

MAJOR =
0
MINOR =
1
PATCH =
0
VERSION =
[MAJOR,MINOR,PATCH].join('.')

Class Method Summary collapse

Class Method Details



16
17
18
# File 'lib/bytes/version.rb', line 16

def self.banner
  "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

.rootObject



20
21
22
# File 'lib/bytes/version.rb', line 20

def self.root
  "#{File.expand_path( 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

.versionObject



12
13
14
# File 'lib/bytes/version.rb', line 12

def self.version
  VERSION
end