• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 752
  • Last Modified:

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
ed

inFile = File.open("variables.txt", "r")
vars = {}
inFile.readlines().each do |line|
  line.scan(/^(\S+)\s*=\s*(\S+).*$/) do |key,val|
    vars[key] = val
     end
  end
inFile.close
method1(vars["var1"],vars["var2"],v]ars["var3"],vars["var4"])
#End ruby script

Which outputs:
value1
value2
value3
value4

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 = File.new(mFileName, "r")
  inFileC = inFile.readlines(rFileName)
  inFile.close
  return inFIleC
end

commands = runFile(input.txt)
commands.each do |i|
  i
end
#End Ruby Script
0
ignusb
Asked:
ignusb
  • 4
  • 3
1 Solution
 
fridomCommented:
load  file?
load "whatever"
or require?

Or what am I missing?

Regards
Friedrich

0
 
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:
file.open(input.txt)
execute(input.txt) as if it were written in the script
#end pseudo script
0
 
fridomCommented:
Well  you can try eval on each line you read.

Regards
Friedrich
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

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

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

Any suggestions as to how to make that work?
0
 
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
0
 
fridomCommented:
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 = File.open("t1.txt", "r")
vars = []
inFile.readlines().each do |line|
  eval line
end

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

output:
 ruby use_t1_txt.rb
VAR1 = var1

However it seems you want to use e.g yaml really.
http://ruby.lickert.net/yaml/index.html

Regards
Friedrich

0
 
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now