Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created January 23, 2019 12:10
Show Gist options
  • Save yitsushi/4faa76d11ca6e822d67c50eb5455ac56 to your computer and use it in GitHub Desktop.
Save yitsushi/4faa76d11ca6e822d67c50eb5455ac56 to your computer and use it in GitHub Desktop.
(teaching material)
id_list = [123456789, 234567891, 345678912, 456789123, 567891234, 789123456, 891234567, 912345678]
packed = id_list.pack('L*')
string_list = id_list.map(&:to_s).join(' ')
puts "Packed version: #{packed.length} bytes long"
puts "Original:"
p id_list
puts "Bytes to send:"
p packed
puts "Simple unpack (Note: they are already numbers):"
rev = packed.unpack('L*')
p rev
puts ""
puts "Space Separated List version: #{string_list.length} bytes long"
puts "Original:"
p id_list
puts "Bytes to send:"
p string_list
puts "Simple split (Note: check the type they are string):"
rev = string_list.split(' ')
p rev
puts "Fully restored:"
rev = string_list.split(' ').map(&:to_i)
p rev
puts ''
puts "Let's see a bit more data"
id_list = []
bound = 1_000_000_000
1000.times do
id_list << (rand() * bound % bound).to_i + bound
end
puts "Number if IDs: #{id_list.length}"
puts "First 5 values: #{id_list[0...5]}"
packed = id_list.pack('L*')
string_list = id_list.map(&:to_s).join(' ')
puts "Packed version: #{packed.length} bytes long"
puts "Space Separated List version: #{string_list.length} bytes long"
puts ''
puts "Let's see different data"
puts "User:\n id: ulong\n username: String\n bday_y: int, bday_m: int, bday_d: int"
require 'json'
class User
def initialize(id, name, b_y, b_m, b_d)
@id = id
@name = name
@bday_y = b_y
@bday_m = b_m
@bday_d = b_d
end
def to_b
[@id, @name, @bday_y, @bday_m, @bday_d].pack('LZ*SCC')
end
def self.from_b(data)
self.new(*data.unpack('LZ*SCC'))
end
def to_json
{ id: @id,
name: @name,
bday_y: @bday_y,
bday_m: @bday_m,
bday_d: @bday_d,
}.to_json
end
def self.from_json(data)
d = JSON.parse(data)
self.new(*JSON.parse(data).values)
end
def to_arr_json
[@id, @name, @bday_y, @bday_m, @bday_d].to_json
end
def self.from_arr_json(data)
d = JSON.parse(data)
self.new(*JSON.parse(data))
end
end
user = User.new(1376262393, 'alice', 1867, 1, 23)
puts "Original:"
p user
puts ''
packed = user.to_b
puts "Packed version: #{packed.length} bytes long"
p packed
puts 'Reverse:'
p User.from_b(packed)
puts ''
json_v = user.to_json
puts "JSON version: #{json_v.length} bytes long"
p json_v
puts 'Reverse:'
p User.from_json(json_v)
puts ''
json2_v = user.to_arr_json
puts "JSON (as array) version: #{json2_v.length} bytes long"
p json2_v
puts 'Reverse:'
p User.from_arr_json(json2_v)
=begin
Output:
Packed version: 32 bytes long
Original:
[123456789, 234567891, 345678912, 456789123, 567891234, 789123456, 891234567, 912345678]
Bytes to send:
"\x15\xCD[\a\xD38\xFB\r@\xA4\x9A\x14\x83\f:\e\"U\xD9!\x80\x11\t/\a)\x1F5NJa6"
Simple unpack (Note: they are already numbers):
[123456789, 234567891, 345678912, 456789123, 567891234, 789123456, 891234567, 912345678]
Space Separated List version: 79 bytes long
Original:
[123456789, 234567891, 345678912, 456789123, 567891234, 789123456, 891234567, 912345678]
Bytes to send:
"123456789 234567891 345678912 456789123 567891234 789123456 891234567 912345678"
Simple split (Note: check the type they are string):
["123456789", "234567891", "345678912", "456789123", "567891234", "789123456", "891234567", "912345678"]
Fully restored:
[123456789, 234567891, 345678912, 456789123, 567891234, 789123456, 891234567, 912345678]
Let's see a bit more data
Number if IDs: 1000
First 5 values: [1095731675, 1978550145, 1256689166, 1600921575, 1945944619]
Packed version: 4000 bytes long
Space Separated List version: 10999 bytes long
Let's see different data
User:
id: ulong
username: String
bday_y: int, bday_m: int, bday_d: int
Original:
#<User:0x007fd0e1024248 @id=1376262393, @name="alice", @bday_y=1867, @bday_m=1, @bday_d=23>
Packed version: 14 bytes long
"\xF9\x18\bRalice\x00K\a\x01\x17"
Reverse:
#<User:0x007fd0e08e38d0 @id=1376262393, @name="alice", @bday_y=1867, @bday_m=1, @bday_d=23>
JSON version: 69 bytes long
"{\"id\":1376262393,\"name\":\"alice\",\"bday_y\":1867,\"bday_m\":1,\"bday_d\":23}"
Reverse:
#<User:0x007fd0e08e1df0 @id=1376262393, @name="alice", @bday_y=1867, @bday_m=1, @bday_d=23>
JSON (as array) version: 30 bytes long
"[1376262393,\"alice\",1867,1,23]"
Reverse:
#<User:0x007fd0e08e0180 @id=1376262393, @name="alice", @bday_y=1867, @bday_m=1, @bday_d=23>
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment