Ruby console extraction


#1

Hello,
I would like to extract in .csv or .txt some results from the ruby console.
Do you know how to do this ?

Thank’s for your help


#2

If you use a “print” statement to write to the console and separate your fields with tabs, you can easily cut and paste it into Excel (or a text file for import). For example, if you need x, ,y and z co-ordinate info, use something like:

print x.to_s + “\t” + y.to_s + “\t” + z.to_s + “\n”

to produce:

0.0 0.0 0.0
0.5 0.333333333333333 0.166666666666667
1.0 0.666666666666667 0.333333333333333
1.5 1.0 0.5
2.0 1.33333333333333 0.666666666666667
2.5 1.66666666666667 0.833333333333333

The “\t” is interpreted as a tab and the “\n” is a carriage return. After highlighting the output text, I found that I needed to right-click in the console window and select “Copy” … ^C didn’t seem to work.

If you want to use CSV format, use something like this:

print “”" + x.to_s + “”,"" + y.to_s + “”,"" + z.to_s + “”\n"

to produce:

“0.0”,“0.0”,"0.0"
“0.5”,“0.333333333333333”,"0.166666666666667"
“1.0”,“0.666666666666667”,"0.333333333333333"
“1.5”,“1.0”,"0.5"
“2.0”,“1.33333333333333”,"0.666666666666667"
“2.5”,“1.66666666666667”,“0.833333333333333”

The backslash character is the escape character for the double-quote mark (").


#3

Some tips:

  • print does not create a new line, that’s why you added the new line character \n to the string. If you log with the puts method, you always get a new line.
  • Ruby has something called “string interpolation”. Instead of creating many single strings and concatenating them (with +), you can directly embed expressions into one string.
  • As string delimiters you could also use pairs of single quotes instead of double quotes (except when we do string interpolation). Only characters that are used as string delimiters need to be escaped, so a double quote character "\"" is the same as '"', and a single quote character '\'' is the same as "'".
  • In this forum, one can wrap text into single backticks ` to mark it as inline source code. If you wrap it into triple backticks on an empty line ` `````` ` `````` ` you get nice code blocks. See more about discourse formatting and markdown.

Then you can write

print "\"" + x.to_s + "\",\"" + y.to_s + "\",\"" + z.to_s + "\"\n"

as

puts("\"#{x}\",\"#{y}\",\"#{z}\"")

If you want to log text directly into a file, you can use Ruby to create a file:

filepath = UI.savepanel()
if filepath
  File.open(filepath, "w") { |file|
    result = 42**2
    file.puts("And the result is: #{result}")
  }
end

#4

One other option … you can paste the following into the Ruby console and all subsequent output will go to the file you specify (note that the directory must exist, but the file need not):

$stdout = File.new('c:\temp\console.txt','w')

When you’re done, enter:

$stdout.close

and then the console window will become active again.

PS: Thanks Aerilius for the tips, especially the backticks :smile:


#5

If you re-assign $stdout, you need to restore it when you are done.


$stdout = SKETCHUP_CONSOLE

#6

Good catch, Jim … somewhere in the process of testing various ways to simplify re-directing the console output, one of my test plugins was affecting the console environment when it loaded at startup. The two steps I outlined above worked for me repeatedly, so I didn’t bother to re-assign $stdout (one less step to perform). However, once I deleted the unneeded test plugin, it started giving stream errors from the lack of a re-assignment.


#7

I have a similar problem,
a script allows me to create a report that appears in the ruby console.
I would like to create a txt file with this report.
With the previous posts I can create a new file but not to write my report in this one.
How am I supposed to do?


#8

Which script? Have you tried contacting the author?


#9

I use Shadowprojector (TIG) the lastest version. and yes i already try to contact him but i don’t have a reponse


#10

I have updated Shadowprojector at your request [to v1.3]…
To see the data, type into the Ruby Console:
TIG.shadowReport
All of the Shadows groups’ attribute data is printed out, formated for copying into another document…
http://sketchucation.com/forums/viewtopic.php?p=546516#p546516

However, I have now updated it to v1.4, now you can get the output of the TIG.shadowReport in the Ruby Console, and also redirect it into a file…
Simply add true after the command - thus: TIG.shadowReport true
You are then prompted for the report file’s destination/name.
Unless you cancel at that point the report is written to the specified file, and it is then opened so you can read it…
Here is the announcement:
http://sketchucation.com/forums/viewtopic.php?p=549974#p549974

Get the latest version [v1.4] from the SketchUcation PluginStore:
http://sketchucation.com/pluginstore?pln=TIG_shadowProjector

Look at the report making code to see how it does it…


#11

Thank’s you a lot TIG
Thank you a lot for your precious help