How to trigger commands in ruby read from an external file?

This is purely a self learning exercise, I'm not sure whether this is going to be possible or not, though I'm sure it would allow for some great flexibility in future code.

What I want, is to load a txt file in, and execute it line by line as if it were ruby code, for example, instead of this:

#Note: This script from Gertone, from my last question.
def method1(var1, var2, var3, var4)
  puts var1
  puts var2
  puts var3
  puts var4

inFile ="variables.txt", "r")
vars = {}
inFile.readlines().each do |line|
  line.scan(/^(\S+)\s*=\s*(\S+).*$/) do |key,val|
    vars[key] = val
#End ruby script

Which outputs:

I'd like to do the same thing, but instead of just reading the variable from the text file using a regex, to execute the text file as if it were a ruby script, i.e. if the text file was:

#begin txt file
var1 = value1
var2 = value2
var3 = value3
var4 = value4
puts var1
puts var2
puts var3
puts var4
#end txt file

For the ruby script to load the text file then execute it.

Is this possible in a scalable manner?

As a note, this is the way I attempted it so far, which I didn't expect to work.. it met expectations:

#Begin Ruby Script
def runFile(mFileName)
  inFile =, "r")
  inFileC = inFile.readlines(rFileName)
  return inFIleC

commands = runFile(input.txt)
commands.each do |i|
#End Ruby Script
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

load  file?
load "whatever"
or require?

Or what am I missing?


ignusbAuthor Commented:
I'm wanting to take it from a txt file.

The only function I have for it at the moment is variable assignment, which can very well be done with the method provided earlier, but I'm just wanting to know if it can be done.

#pseudo script:
execute(input.txt) as if it were written in the script
#end pseudo script
Well  you can try eval on each line you read.

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

ignusbAuthor Commented:
I'm not sure how to use eval.
Having just looked it up, this is what I tried:

inFile ="nToolsMail.txt", "r")
vars = []
inFile.readlines().each do |line|
  eval line

which returned:
private method `test' called for nil:NilClass (NoMethodError)

Any suggestions as to how to make that work?
ignusbAuthor Commented:
ignore the line in that last message which is:

vars = []

that was incorrect.

Also, the text file that is being used as an input is:

var1 = "value1"
var2 = "value2"
var3 = 3
Well you can not expect that anything  you write in  a file can be seen as ruby expression.
But $VAR1 = "value" should do
Howerver I do not feel your approach is a good one. I can not see what you win with it.
but here we go:
content of t1.txt:

$VAR1 = "var1"

content if use_t1_txt.rb
inFile ="t1.txt", "r")
vars = []
inFile.readlines().each do |line|
  eval line

print("VAR1 = #{$VAR1}\n")

 ruby use_t1_txt.rb
VAR1 = var1

However it seems you want to use e.g yaml really.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ignusbAuthor Commented:
Thanks for that.
And as I said, I'm not sure that it ever will have practical application, I'm just learning the language at the moment and it was something that I was curious as to if it would be able to work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.