sketchucation logo sketchucation
    • Login
    πŸ›£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Saving all instance variables of an object

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 4 Posters 1.2k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      abakobo
      last edited by

      Hi

      I decided to use Marshal as I suppose I won't do better! 😜 (though it's version dependent and has some security issues)

      I found some code examples modified and ran it... cool stuff. But there are two things I don't understand in the code I copied!

      
      class MyObject 
      	
      	def initialize(value)  
      		@value = value 
      		@arr= [4,5,6]
      	end  
      	attr_accessor ;value, ;arr
      	
      	def incr
      		@value=@value+1
      	end
      	
      end  
      
      
      obj=MyObject.new(99)
      
      file_name=UI.savepanel("Save","",".oob")
      
      File.open(file_name, 'w+') do |f|
      	Marshal.dump(obj, f) 
      end
      
      
      File.open(file_name) do |f|  
      	@obj2 = Marshal.load(f)  
      end  
      
      obj3=@obj2.clone
      obj3.incr
      puts obj3.value,obj3.arr
      
      

      Why must I use @ or $ for my obj2?

      And why the code below doesn't close the file in comparaison to the one above. Is that the "block form" advised in Dan's post? I miss the mechanics of that "do ||" statement, I tought it was just a looping tool. ---> I read this now: http://blog.rubybestpractices.com/posts/gregory/009-beautiful-blocks.html

      
      f=File.open(file_name, 'w+')
      puts f
      Marshal.dump(obj, f)
      
      #need f.close!
      
      

      Thanks a lot for your answers!

      ako

      1 Reply Last reply Reply Quote 0
      • S Offline
        slbaumgartner
        last edited by

        @abakobo said:

        Why must I use @ or $ for my obj2?
        [

        These prefixes are part of the names of variables. They tell Ruby what scope the variables belong to (@ for attributes of objects, $ for globals - there is also @@ for class variables). Variables without one of these leading characters are local to the code scope where they appear.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          @slbaumgartner said:

          They tell Ruby what scope the variables belong to (@ for attributes of objects, $ for globals

          And for the record - global variables should be avoided. In Extension Warehouse they are an immediate rejection.

          Thomas Thomassen β€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            @thomthom said:

            And for the record - global variables should be avoided. In Extension Warehouse they are an immediate rejection.

            And yet.. the global objectspace is still cluttered with global variables created by SketchUp Team extensions!

            Many of these are for LanguageHandler instances that are no longer needed once the Preferences UI strings have been loaded, or a local extension hash loaded with strings.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              Yea - there's a need for a cleanup for the old SketchUp extensions.

              Thomas Thomassen β€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • A Offline
                abakobo
                last edited by

                I tried to save an object containing an SKUI window object (using marshal) and had a bad type error... I suppose it's not possible to save an SKUI window with marshal due to it's limitations:

                *Marshal can't dump following objects:

                anonymous Class/Module.
                
                objects which related to its system (ex: Dir, File::Stat, IO, File, Socket and so on)
                
                an instance of MatchData, Data, Method, UnboundMethod, Proc, Thread, ThreadGroup, Continuation
                
                objects which defines singleton methods*
                

                I did it manualy this time but wonder if there's something similar to Marshal with what I would be able to save objects containing SKUI windows for later use.

                I remeber i saw somthing with a name looking like "yalm"..
                Would Pstore do it?

                Many Thanks

                ako

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  Classes needs to implement support for marshalling. In the case of SKUI the Window class holds on to references to UI::WebDialog - which cannot be serialized. This also is also the case for Sketchup::Entity derived classes.

                  Btw, why are you trying to marshall a SKUI Window? Are you trying to save the window position?
                  Or are you trying to save the whole state of it - including HTML content and session?

                  Thomas Thomassen β€” SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • A Offline
                    abakobo
                    last edited by

                    I'm trying to save all the .visible? .caption(for lbls) and .value(for inputs) for now.

                    I would like to create a window where there's a "add item" on the window that add a SKUI_group on the window corresponding to the new item...
                    My object have only two instance variables for now, the number of items(an int) and the SKUI window.

                    For the non customisable plugin I could save the .visible? .caption and .value by listing them manualy in my Save method but with a customizable window it would probably be painful...

                    thanks for your answers
                    AND THANKS A LOT FOR SKUI (sorry for shouting but i really mean it)

                    P.S. I noticed a little bug: I have to initialise myself the .visible of each visible unmodified SKUI items before saving them because it was returning nil with the .visible? method if .visible= was never used.

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      @abakobo said:

                      P.S. I noticed a little bug: I have to initialise myself the .visible of each visible unmodified SKUI items before saving them because it was returning nil with the .visible? method if .visible= was never used.

                      Hmm... Could you file this bug in the GitHub repo with a small code snippet?

                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • A Offline
                        abakobo
                        last edited by

                        I never did that but will learn how to.
                        Glad to participate... πŸ˜„

                        1 Reply Last reply Reply Quote 0
                        • A Offline
                          abakobo
                          last edited by

                          Bug posted in the GitHub Issues field!

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement