[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

if /hello/ =~line

Peter Loftus

12/6/2007 11:36:00 AM

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
--
Posted via http://www.ruby-....

11 Answers

Jeremy McAnally

12/6/2007 11:48:00 AM

0

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 Loftus <loftuz@gmail.com> 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
> --
> Posted via http://www.ruby-....
>
>


--
http://www.jeremymca...

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

My free Ruby e-book
http://www.humblelittlerub...

My blogs:
http://www.mrneigh...
http://www.rubyinpra...

Lee Jarvis

12/6/2007 11:50:00 AM

0

Peter Loftus 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
--
Posted via http://www.ruby-....

Peter Loftus

12/6/2007 12:14:00 PM

0

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
>
--
Posted via http://www.ruby-....

Jari Williamsson

12/6/2007 12:25:00 PM

0

Peter Loftus wrote:
> 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

Jeremy McAnally

12/6/2007 12:30:00 PM

0

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/Quick...

Or, optionally, the Pickaxe book section on strings.

--Jeremy

On 12/6/07, Peter Loftus <loftuz@gmail.com> wrote:
> 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
> >
> --
> Posted via http://www.ruby-....
>
>


--
http://www.jeremymca...

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

My free Ruby e-book
http://www.humblelittlerub...

My blogs:
http://www.mrneigh...
http://www.rubyinpra...

Jano Svitok

12/6/2007 12:39:00 PM

0

On Dec 6, 2007 12:49 PM, Lee Jarvis <ljjarvis@gmail.com> 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"
+ 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

Peter Loftus

12/6/2007 12:45:00 PM

0

Thanks for explaining it

Cheers
Loftz

Jeremy McAnally wrote:
> 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/Quick...
>
> Or, optionally, the Pickaxe book section on strings.
>
> --Jeremy
>
> On 12/6/07, Peter Loftus <loftuz@gmail.com> wrote:
>> >
>>
> --
> http://www.jeremymca...
>
> My books:
> Ruby in Practice
> http://www.manning.com...
>
> My free Ruby e-book
> http://www.humblelittlerub...
>
> My blogs:
> http://www.mrneigh...
> http://www.rubyinpra...

--
Posted via http://www.ruby-....

Lee Jarvis

12/6/2007 1:17:00 PM

0

Jano Svitok 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
--
Posted via http://www.ruby-....

yermej

12/6/2007 3:32:00 PM

0

On Dec 6, 6:38 am, Jano Svitok <jan.svi...@gmail.com> wrote:
> On Dec 6, 2007 12:49 PM, Lee Jarvis <ljjar...@gmail.com> 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"
> + 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

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

Jano Svitok

12/6/2007 4:34:00 PM

0

On Dec 6, 2007 4:35 PM, yermej <yermej@gmail.com> wrote:
> 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 ;-)