jazcyk
December 6, 2007, 12:35pm
1
Got help with this code earlier its just checking a file for a line
Im really new to ruby ive used java and C before
just wondering what do i put in if i want to use a variable that is
holding the string eg VAR1 = “hello”
File.foreach “file.txt” do |line|
if /hello/ =~ line
puts “found it”
break
end
end
jazcyk
December 6, 2007, 12:49pm
2
You can use string interpolation like you can with double quoted
strings.
var1 = “hello”
File.foreach “file.txt” do |line|
if /#{var1}/ =~ line
puts “found it”
break
end
end
–Jeremy
On 12/6/07, Peter L. [email protected] wrote:
break
end
end
Posted via http://www.ruby-forum.com/ .
–
http://www.jeremymcanally.com/
My books:
Ruby in Practice
Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...
My free Ruby e-book
My blogs:
http://www.rubyinpractice.com/
jazcyk
December 6, 2007, 12:49pm
3
Peter L. wrote:
Got help with this code earlier its just checking a file for a line
Im really new to ruby ive used java and C before
just wondering what do i put in if i want to use a variable that is
holding the string eg VAR1 = “hello”
File.foreach “file.txt” do |line|
if /hello/ =~ line
puts “found it”
break
end
end
var = “hello”
File.foreach “file.txt” do |line|
if /#{var}/ =~ line
puts “found it”
break
end
end
Perhaps?
Regards,
Lee
jazcyk
December 6, 2007, 1:07pm
5
if /#{var1}/ =~ line
when i enter # thats just comments out the whole line
any reason for using #
Jeremy McAnally wrote:
You can use string interpolation like you can with double quoted
strings.
var1 = “hello”
File.foreach “file.txt” do |line|
if /#{var1}/ =~ line
puts “found it”
break
end
end
jazcyk
December 6, 2007, 1:31pm
6
Though your IDE may think you’re commenting out the whole line, you’re
not. It’s the proper way to do string interpolation.
See here: Ruby | zenspider.com | by ryan davis
Or, optionally, the Pickaxe book section on strings.
–Jeremy
On 12/6/07, Peter L. [email protected] wrote:
–
http://www.jeremymcanally.com/
My books:
Ruby in Practice
Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...
My free Ruby e-book
My blogs:
http://www.rubyinpractice.com/
jazcyk
December 6, 2007, 2:17pm
8
Jano S. wrote:
(This might apply for 1.8 MRI only, other interpreters might be
different): When the regex literal contains #{}, the object
is created on each pass through the code. In the other case it’s
created only once.
True, you could use the Regexp class also then…
var = ‘hello’
revar = Regexp.new(var)
if revar =~ line
#…
end
or perhaps
var = ‘hello’
File.foreach ‘file.txt’ do |line|
if Regexp.new(var) =~ line
puts ‘found it’
break
end
end
If you don’t care how many times its created and you don’t want to use
the ‘#’ in your regexp matches because your IDE doesn’t know any better.
Regards,
Lee
jazcyk
December 6, 2007, 1:39pm
9
On Dec 6, 2007 12:49 PM, Lee J. [email protected] wrote:
Sometimes it’s better to cache the /#{var}/ as in this case, new
regexp object is created on each pass through the cycle.
That might hurt the performance a bit. So:
var = “hello”
File.foreach “file.txt” do |line|
puts "found it"
break
end
end
(This might apply for 1.8 MRI only, other interpreters might be
different): When the regex literal contains #{}, the object
is created on each pass through the code. In the other case it’s
created only once.
Jano
jazcyk
December 6, 2007, 4:35pm
10
On Dec 6, 6:38 am, Jano S. [email protected] wrote:
if /#{var}/ =~ line
created only once.
Jano
You can also use the /o option, which tells Ruby to compile the Regex
only once:
var = “hello”
File.foreach “file.txt” do |line|
if /#{var}/o =~ line
puts “found it”
break
end
end
jazcyk
December 6, 2007, 5:35pm
11
On Dec 6, 2007 4:35 PM, yermej [email protected] wrote:
end
Thanks, I didn’t know that… Now I see it even in the first pickaxe
book… I guess I should read it once more
jazcyk
December 6, 2007, 8:25pm
12
On Dec 6, 2007 5:17 AM, Lee J. [email protected] wrote:
True, you could use the Regexp class also then…
var = ‘hello’
revar = Regexp.new(var)
And don’t miss the very useful Regexp.quote, which makes sure
everything in var is matched literally
revar = Regexp.new(Regexp.quote(var))
martin