Last active
August 9, 2018 16:02
-
-
Save havenwood/1e0111232b3e441d3ec60f103212508a to your computer and use it in GitHub Desktop.
Response to TvL2386 on the #ruby IRC channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'psych' | |
class MailTruck | |
attr_accessor :driver, :route | |
def initialize driver:, route:, speed: 0 | |
@driver = driver | |
@route = route | |
@speed = speed | |
end | |
def encode_with coder | |
coder['driver'] = @driver | |
coder['route'] = @route | |
end | |
def _dump _ | |
Marshal.dump({driver: @driver, route: @route}) | |
end | |
class << self | |
def init_with coder | |
@driver = coder['driver'] | |
@route = coder['route'] | |
end | |
def _load s | |
new **Marshal.load(s) | |
end | |
end | |
end | |
truck = MailTruck.new( | |
driver: 'Harold', | |
route: ['12 Corrigan Way', '23 Antler Ave'] | |
) | |
yaml = truck.to_yaml | |
puts yaml | |
#>> --- !ruby/object:MailTruck | |
#>> driver: Harold | |
#>> route: | |
#>> - 12 Corrigan Way | |
#>> - 23 Antler Ave | |
Psych.load yaml | |
#=> #<MailTruck:... @driver="Harold", @route=["12 Corrigan Way", "23 Antler Ave"]> | |
marshal = Marshal.dump truck | |
#=> "\x04\bu:\x0EMailTruckR\x04\b{\a:\vdriverI\"\vHarold\x06:\x06ET:\nroute[\aI\"\x1412 Corrigan Way\x06;\x06TI\"\x1223 Antler Ave\x06;\x06T" | |
Marshal.load marshal | |
#=> #<MailTruck:0x000000010a1dfc58 @driver="Harold", @route=["12 Corrigan Way", "23 Antler Ave"], @speed=0> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment