Create a text file

edited February 2018 in Ruby Scripting
Hi Matthias,

I would like to know if it is possible to create a text file with a Ruby script. My idea is to get a summary of parameters and calculations at the end of the macro execution.
Example:
Project name: xxxx
Area: yyyy
CD: zzzz
....

Thanks in advance for your answer.
Eric

Comments

  • edited February 2018

    Outputting a text file from Ruby can be done in several ways -- search google for other examples if the below doesn't suit.

    I will also note that there is a relatively easy way to just get a hash with all of the variables in it. So, if in your code you've calculated project_name, area, cd, ..., then you run this code to generate a hash with everything you want, then output it to txt file.

    project_name = 'something'
    area = 1.1
    cd = 1.2
    
    hash = {}
    local_variables.each{ |sym|
      var = "#{sym}"
      val = eval(var)
    
      unless var == "hash" # ie. ignore the variable called "hash"
        hash[var] = val # append it to the hash
      end
    }
    
    # Print out the result to the console
    p hash
    
    # The write that hash to a file
    yourfile = 'C:\Users\davidnhutch\Downloads\something.txt'
    File.open(yourfile, 'w') { |file| 
      file.write(hash) # I haven't attempted to format this in the same way you did, but that can be done
    }
    
  • edited November -1
    Thank you David for your answer and your proposal.

    I used the following code that it perfectly works for my need:

    summaryfile = 'C:\Users\xxxxh\KLayout\Rep_travail\Template\Summary.txt'
    File.open(summaryfile, 'w') do |out|

    out << "Project name: #{project_name.text} \n"
    out << "Design area : #{area} \n"
    out << "CD: #{cd} \n"
Sign In or Register to comment.