Guys,
I want to read the file content and store them in different
variables.
My file looks something like this:
url = “http://localhost”
emailid = “[email protected]”
password= “12345678”
browser=“iexplore”
I have to read individual lines but store only values within the quotes
(values after =)
I heard in java there is some get property which picks values after ‘=’
and so on.
Let me know if there is any ruby equivalent to this or any other method
to get this done.
eg:
variables id = [email protected] & so on…
cheers
A lot of people use YAML, a different format like this:
url: http://localhost
emailid: [email protected]
password: “12345678”
browser: iexplore
However personally I dislike YAML because it’s very data-sensitive (note
the need for quotes around the password above) and sometimes buggy.
If you google for “ruby ini file” you’ll find some code which probably
does what you need. Otherwise, it’s pretty easy to roll your own:
conf = {}
File.open(“conf.ini”) do |source|
source.each_line do |line|
if line =~ /^(\w+)\s*=\s*"(.)"\s$/
conf[$1] = $2
end
end
end
Here I build a Hash, so you can access it as
conf[‘url’]
But you could build an OpenStruct instead (look at ostruct.rb) so you
could access it as conf.url
2009/8/10 Brian C. [email protected]:
If you google for “ruby ini file” you’ll find some code which probably
Here I build a Hash, so you can access it as
conf[‘url’]
But you could build an OpenStruct instead (look at ostruct.rb) so you
could access it as conf.url
You can save one level of indentation by using
File.foreach “conf.ini” do |line|
…
end
And if you want to get really short (not necessarily recommended):
conf = {}
File.foreach “conf.ini” do |line|
conf[$1] = $2 if /^\s*(\w+)\s*=\s*“([^”])"\s$/ =~ line
end
Kind regards
robert
I heard in java there is some get property which picks values after ‘=’
and so on.
Let me know if there is any ruby equivalent to this or any other method
to get this done.
Another way.
Modify as necessary.
source = <<EOF
url = “http://localhost”
emailid = “[email protected]”
password= “12345678”
browser=“iexplore”
EOF
require ‘shellwords’
include Shellwords
h = Hash[*(shellwords(source.gsub(“=”," ")))]
“browser”=>“iexplore”, “password”=>“12345678”}
Harry
Joel VanderWerf wrote:
Harry K. wrote:
h = Hash[*(shellwords(source.gsub("="," ")))]
source.gsub(/^([^=]+)=/) {$1}
sorry, should be:
source.gsub(/^([^=]+)=/) {$1 + " "}
Joel VanderWerf wrote:
Joel VanderWerf wrote:
Harry K. wrote:
h = Hash[*(shellwords(source.gsub(“=”," ")))]
source.gsub(/^([^=]+)=/) {$1}
sorry, should be:
source.gsub(/^([^=]+)=/) {$1 + " "}
Hi,
Using source.gsub(/^([^=]+)=/) {$1 + " "} gives me the below shown
result:
url “http://localhost”
emailid “[email protected]”
password “12345678”
browser “iexplore”
Since i am new to programming lang, i dont know how to write a regex.
I am looking for output like this
a = “http://localhost”
b = “[email protected]”
where a & b are variables i am storing these values into. let me know if
i am not clear on this.
Cheers
Harry K. wrote:
h = Hash[*(shellwords(source.gsub("="," ")))]
source.gsub(/^([^=]+)=/) {$1}
in case there is another = char on the line.
Nice to know about shellwords for this purpose, thanks!
7stud – wrote:
Shekar Ls wrote:
Since i am new to programming lang, i dont know how to write a regex.
I am looking for output like this
a = “http://localhost”
b = “[email protected]”
where a & b are variables i am storing these values into.
You would never do that.
Well, I guess that’s not true. You can do this:
results = [“hello”, “hi”, “goodbye”]
a, b, c = results
puts a, b, c
–output:–
hello
hi
goodbye
Shekar Ls wrote:
Since i am new to programming lang, i dont know how to write a regex.
I am looking for output like this
a = “http://localhost”
b = “[email protected]”
where a & b are variables i am storing these values into.
You would never do that. You store multiple values in arrays. For
instance,
data = [“hello”, “hi”, “goodbye”]
puts data[1]
–output:–
“hi”
You can get the data out of your file without using regex’s like this:
results = []
IO.foreach(“data.txt”) do |line|
pieces = line.split(“=”)
val = pieces[1].strip #remove spaces on the left and the right
val = val[1…-1] #slice off the quotes
results << val
end
p results
–output:–
[“http://localhost”, “[email protected]”, “12345678”, “iexplore”]
7stud – wrote:
7stud – wrote:
Shekar Ls wrote:
Since i am new to programming lang, i dont know how to write a regex.
I am looking for output like this
a = “http://localhost”
b = “[email protected]”
where a & b are variables i am storing these values into.
You would never do that.
Well, I guess that’s not true. You can do this:
results = [“hello”, “hi”, “goodbye”]
a, b, c = results
puts a, b, c
–output:–
hello
hi
goodbye
HI,
Let me be more clear over the requirement , my intention to read from
a remote file & store the values into a variable so that i can make use
of these variables for my automation.
let me try reading this array using a File.open if it works!!
Robert D. wrote:
On Mon, Aug 10, 2009 at 9:20 PM, Joel VanderWerf[email protected] wrote:
source.gsub(/^([^=]+)=/) {$1 + " "}
I was just wondering, should we not refuse to use ^ for the beginning
of a string ( even knowing it is a line )? I personally have abandoned
^ for \A for quite some time now. Is nobody going to follow ;).
Didn’t the OP want each line converted? It’s getting hard to remember…
Anyway, I did mean ^ in my answer, but maybe I was confused about the
question.
2009/8/17 Joel VanderWerf [email protected]:
Didn’t the OP want each line converted? It’s getting hard to remember…
Anyway, I did mean ^ in my answer, but maybe I was confused about the
question.
Sorry my bad. I did not realize from the snippet that you were doing
the whole source in one thing. But I should have, given the gsub.
Yes of course you need to use ^.
As a matter of fact my point was not to use it on lines, because ^
matches at the beginning of a line while when we are treating lines
there are no lines anymore and we want to match at the beginning of a
string, hence \A.
Cheers
Robert
Robert D. wrote:
I was just wondering, should we not refuse to use ^ for the beginning
of a string ( even knowing it is a line )? I personally have abandoned
^ for \A for quite some time now. Is nobody going to follow ;).
I didn’t follow you, but I’ve been using \A…\z for years.
This is certainly an area where Ruby can trip you up dangerously, if
you’re expecting Perl-like regular expression behaviour.
On Mon, Aug 10, 2009 at 9:20 PM, Joel
VanderWerf[email protected] wrote:
source.gsub(/^([^=]+)=/) {$1 + " "}
–
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
I was just wondering, should we not refuse to use ^ for the beginning
of a string ( even knowing it is a line )? I personally have abandoned
^ for \A for quite some time now. Is nobody going to follow ;).
Just my 0.03¤ ( yeah inflation )
R.