if /hello/ =~line

P

Peter Loftus

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
 
J

Jeremy McAnally

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

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


--
http://www.jeremymcanally.com/

My books:
Ruby in Practice
http://www.manning.com/mcanally/

My free Ruby e-book
http://www.humblelittlerubybook.com/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
 
L

Lee Jarvis

Peter said:
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
 
P

Peter Loftus

if /#{var1}/ =~ line

when i enter # thats just comments out the whole line
any reason for using #
 
J

Jari Williamsson

Peter said:
if /#{var1}/ =~ line

when i enter # thats just comments out the whole line
any reason for using #

# is not a comment inside a regexp or string


Best regards,

Jari Williamsson
 
J

Jeremy McAnally

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: http://www.zenspider.com/Languages/Ruby/QuickRef.html#6

Or, optionally, the Pickaxe book section on strings.

--Jeremy

if /#{var1}/ =~ line

when i enter # thats just comments out the whole line
any reason for using #


--
http://www.jeremymcanally.com/

My books:
Ruby in Practice
http://www.manning.com/mcanally/

My free Ruby e-book
http://www.humblelittlerubybook.com/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
 
J

Jano Svitok

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" + var_re = /#{var}/
File.foreach "file.txt" do |line|
- if /#{var}/ =~ line
+ if var_re =~ 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
 
P

Peter Loftus

Thanks for explaining it

Cheers
Loftz

Jeremy said:
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: http://www.zenspider.com/Languages/Ruby/QuickRef.html#6

Or, optionally, the Pickaxe book section on strings.

--Jeremy


--
http://www.jeremymcanally.com/

My books:
Ruby in Practice
http://www.manning.com/mcanally/

My free Ruby e-book
http://www.humblelittlerubybook.com/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
 
L

Lee Jarvis

Jano said:
(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
 
Y

yermej

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:


- if /#{var}/ =~ line
+ if var_re =~ line


(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

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
 
J

Jano Svitok

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

Thanks, I didn't know that... Now I see it even in the first pickaxe
book... I guess I should read it once more ;-)
 
M

Martin DeMello

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,365
Members
48,050
Latest member
Carson62C3

Latest Threads

Top