Ruby console extraction

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

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:


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

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"



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

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

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 ='c:\temp\console.txt','w')

When you’re done, enter:


and then the console window will become active again.

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

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


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.

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?

Which script? Have you tried contacting the author?

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

I have updated Shadowprojector at your request [to v1.3]…
To see the data, type into the Ruby Console:
All of the Shadows groups’ attribute data is printed out, formated for copying into another document…
[Plugin] ShadowProjector • sketchUcation • 2

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:

Get the latest version [v1.4] from the SketchUcation PluginStore:

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

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