sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Input and Output files in Ruby

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 2 Posters 359 Views 2 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.
    • Z Offline
      Zane
      last edited by

      Basically i've tried to do some research on this but I can't seem to get anything that is working in Sketchup so I really need your help guys, here is the problem i'm having:

      I have a text file in my documents which contain three numbers (they can either be seperated by spaces or by /n) and I want to have ruby read that file in line by line, storing each line as a new variable, the first variable being depth, the second being width and the third being length.

      Do you guys think you can help? I can show you what I mean in C++ if that helps:

      
      ifstream input("My Documents\\slabout.text");
      input >> depth >> width >> length
      input.close();
      
      

      Thank you for your help!

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

        Hi Zane,

        You may want to check out Dan's reading a CSV file post.

        But in short and ignoring Exceptions and namespace issues, the core code might be similar to...

        
        # lines will reference a String including newlines.
        lines = IO.read("My Documents\\slabout.text")
        width, depth, height = lines.split("\n")
        
        

        Then, you will probably need to convert the values to Floats or Integers to make them useful...

        
        width = width.to_f
        ...
        
        

        Or reading each line..

        IO.foreach("My Documents\\slabout.text") do |line|
          # do something with line variable (String)
        end
        

        Hi

        1 Reply Last reply Reply Quote 0
        • Z Offline
          Zane
          last edited by

          That's fantastic Jim, thanks!

          I just happened to stumble across something similar where I can use IO.readlines and arrays to sort out the writing in lines one by one.

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

            Just another thought (while browsing around the forums..)

            
            depth = width = length = nil
            File.open("My Documents\\slabout.text") { |fp|   
              depth, width, length = fp.gets, fp.gets, fp.gets
            }
            
            

            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