From: Patrick Okui Date: 2009-08-19T19:29:52+09:00 Subject: Re: copying values from a different file into variables On Aug 18, 2009, at 10:23 AM, Shekar Ls wrote: > I would like to copy specific lines(contents) from file1 into file2. You'd generally get more help if you try to do some of the work and then post if/when you get stuck. > > # File1 has following information: > > browser = "firefox" > id = "test@testuser.com" > password= "12345678" > account = "Yes" > > > # I want the following information in File2 : > > @var1 = "firefox" (value of browser from File1) > @var2 = "test@testuser.com" (value of id from File1) > @var3 = "12345678" (value of password from File1) > > How do i get this done using ruby. Please let me know if you require > more clarification on this front. I haven't used ruby that long but something like File.open("file2", "w") do |f2| count = 1 File.foreach("file1") do |line| f2 << line.sub(/^[\w\s]+=/, "@var#{ count } =") count += 1 end end could work. (skipping the creation of @var4 for the "account" line is left as an exercise for the OP) -- patrick