sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Ruby right write

    Scheduled Pinned Locked Moved Developers' Forum
    13 Posts 4 Posters 471 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.
    • thomthomT Offline
      thomthom
      last edited by

      hmm..
      I never use the IO class directly to read/write files.

      I use the File class's methods.

      
      # Read Files
      f = File.open(file, 'rb')
      # read from file
      f.close
      
      
      
      # Write Files
      f = File.open(file, 'w')
      f.puts 'hello world'
      f.close
      
      

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

      1 Reply Last reply Reply Quote 0
      • chrisglasierC Offline
        chrisglasier
        last edited by

        Thanks Thom. Actually Jim wrote that and he should be asleep. I don't understand (File.join(File.dirname(__FILE__), subDir, file)) - why it has to be joined. Do I need to know. Is it relevant to writing?

        Thanks

        Chris

        With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

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

          The join method ensures that when you combine parts of a filename that it's compiled correctly, with forward slashes etc /. It return a fully compiled path to your file.

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

          1 Reply Last reply Reply Quote 0
          • chrisglasierC Offline
            chrisglasier
            last edited by

            Thanks again

            With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

            1 Reply Last reply Reply Quote 0
            • chrisglasierC Offline
              chrisglasier
              last edited by

              One other thing has come from this. I can write to the same file using Ruby or activeXObject in an .hta. The Ruby version adds an extra empty line which throws an "unterminated string constant" error when read. Any idea about what is going on?

              Thanks

              
              function coreRubyReq(callback, params) {
              	window.location = "skp;" + callback + "@" + params.join(';');
              }
              
              function coreJsonSave(fileName, json) {
              	var ForWriting, objFSO, objFile, txt;
              	txt = JSON.stringify(json);
              	if(config.SU === 1){
              		coreRubyReq('save_json', [fileName, txt]);
              	}
              	else{
              		ForWriting = 2;
              		objFSO = new ActiveXObject("Scripting.FileSystemObject");
              		objFile = objFSO.CreateTextFile(fileName, ForWriting);
              		objFile.Write( txt );
              		objFile.Close();
              	}
              }
              
              
              
              @dlg.add_action_callback("save_json") { |d, a|
              			p = a.split(";")
              			subDir = "nset/Sketchup/"
              			file = File.join(File.dirname(__FILE__), subDir, p[0])
              			f = File.open(file, 'w')
              			f.puts p[1].to_s
              			f.close
              		}
              
              

              With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

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

                hmm...

                extra line? at the end? or for each line?

                Could be that my example opened the file as binary.

                #binary read
                f = File.open(file, 'rb')
                
                #normal read
                f = File.open(file, 'r')
                

                This the latter treats line breaks differently. (linebreaks are different from platform to platform)

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

                1 Reply Last reply Reply Quote 0
                • chrisglasierC Offline
                  chrisglasier
                  last edited by

                  machine Jan  008.png

                  I left Jim's for the read and used yours for the write.

                  With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                  1 Reply Last reply Reply Quote 0
                  • chrisglasierC Offline
                    chrisglasier
                    last edited by

                    Maybe if I pass it though a hidden input?? I should do that anyway - forgot!

                    With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

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

                      ? to get rid of extra lines?

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

                      1 Reply Last reply Reply Quote 0
                      • C Offline
                        cjthompson
                        last edited by

                        @chrisglasier said:

                        
                        > @dlg.add_action_callback("save_json") { |d, a|
                        > 			p = a.split(";")
                        > 			subDir = "nset/Sketchup/"
                        > 			file = File.join(File.dirname(__FILE__), subDir, p[0])
                        > 			f = File.open(file, 'w')
                        > 			f.puts p[1].to_s
                        > 			f.close
                        > 		}
                        > 
                        

                        I think the problem is

                        f.puts p[1].to_s
                        

                        Try using

                        f.write p[1].to_s
                        
                        1 Reply Last reply Reply Quote 0
                        • chrisglasierC Offline
                          chrisglasier
                          last edited by

                          Works ... thanks Chris

                          Chris

                          With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                          1 Reply Last reply Reply Quote 0
                          • J Offline
                            Jim
                            last edited by

                            I think it's odd that the parser would be sensitive to a newline at the end of the file.

                            Hi

                            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