Connecting Tech Pros Worldwide Help | Site Map

Reading contents of a file and storing into variables

Newbie
 
Join Date: Dec 2008
Posts: 2
#1: Aug 14 '09
Hi,
I want to read the file content and store them in different
variables.
My file looks something like this:

----------------------------------------------------
url = "http://localhost"
emailid = "User@htmdec.com"
password= "12345678"
browser="iexplore"
-------------------------------------------------

I have to read each line and store only values within quotes into a variable.

eg:
webpage = "http://localhost"
pwd = "12345678"

Let me know if there is any ruby equivalent to this or any other method
to get this done.

Cheers
Expert
 
Join Date: May 2007
Posts: 213
#2: Aug 14 '09

re: Reading contents of a file and storing into variables


You can read the file using File.read. Then you will have to get the necessary data out of each line.
Expand|Select|Wrap|Line Numbers
  1. File.read(filename).each{|line|
  2.   # Perform necessary manipulation in here
  3. }
Newbie
 
Join Date: Dec 2008
Posts: 2
#3: Aug 17 '09

re: Reading contents of a file and storing into variables


Thanks for your reply, More than reading a file, i wanted to know is there a way to selective pick the information within quotes(" ") and storing such information in a variable.
Is there a regex that i need to implement to get the value or is there any easier way of fetching values and storing them.
Please do give me a example as i am new to ruby.

cheers
Expert
 
Join Date: May 2007
Posts: 213
#4: Aug 17 '09

re: Reading contents of a file and storing into variables


One way to get the info from within quotes would be to split the string on the quote character. This would split your string into three pieces, so you would want the middle one.
Expand|Select|Wrap|Line Numbers
  1. s.split('"')[1]
Reply