Home > Mobile >  How to copy object from one shell to another shell?
How to copy object from one shell to another shell?

Time:05-05

You have two open IRB shells in separate terminal windows. The first shell has the following code already entered:

class Apple

  attr_reader :variety, :origin, :history
  def initialize(**args)
    @variety = args[:variety]
    @origin = args[:origin]
    @history = args[:history]
  end
end

apple = Apple.new(variety: 'Honeycrisp', origin: 'Minnesota', history: 'Introduction to Market: 1991')

The second shell has the following code already entered:

class Apple

  attr_reader :variety, :origin, :history
  def initialize(**args)
    @variety = args[:variety]
    @origin = args[:origin]
    @history = args[:history]
  end
end

Write the code in the shell(s) that copies an object apple from shell one to shell two. Bonus: write the solution without using external Ruby libraries.

CodePudding user response:

You can use Marshal for that, in one terminal run.

Marshal.dump(apple)

Copy the string returned.

str = [copied from the other terminal]
apple = Marshal.load(str)
  • Related