Skip to content

Instantly share code, notes, and snippets.

@e-jambon
Created June 24, 2016 08:16
Show Gist options
  • Save e-jambon/f2da46ddbef2aadbcdc464b935b6e033 to your computer and use it in GitHub Desktop.
Save e-jambon/f2da46ddbef2aadbcdc464b935b6e033 to your computer and use it in GitHub Desktop.

Revisions

  1. e-jambon revised this gist Jun 24, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Marshalling_struct.rb
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    def action
    puts "data = " + data.inspect
    end

    def save_to filepath
    File.open( filepath, 'w+' ) do |f|
    Marshal.dump(self,f)
  2. e-jambon created this gist Jun 24, 2016.
    44 changes: 44 additions & 0 deletions Marshalling_struct.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    ItemMenu = Struct.new(:lvl, :description, :command_type, :data) do


    def action
    puts "data = " + data.inspect
    end
    def save_to filepath
    File.open( filepath, 'w+' ) do |f|
    Marshal.dump(self,f)
    end
    end

    def load_from filepath
    File.open(filepath, 'r') do |f|
    tmp = Marshal.load(f)
    puts "tmp : " + tmp.inspect
    self.lvl = tmp.lvl
    self.description = tmp.description
    self.command_type = tmp.command_type
    self.data = tmp.data
    end
    end

    end



    l_1 = ItemMenu.new( '0.1', 'First Item','shell', 'ls' )
    puts ' == CREATION == '
    puts l_1.lvl
    puts l_1.description
    puts l_1.command_type
    puts l_1.data
    puts l_1.action



    puts ' == SAVING == '
    l_1.save_to '/home/username/work/menu/data/l_1.txt'

    puts '== LOADING == '
    l_2 = ItemMenu.new
    l_2.load_from '/home/username/work/menu/data/l_1.txt'
    puts l_2.inspect