Input and Output files in Ruby
-
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!
-
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 -
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.
-
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 }
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better đź’—
Register LoginAdvertisement