Regexp.match(line) question

Hi,
I new to Ruby. It's a lot of fun to learn..

if I do this:

f=open('filename.dat','r')
str=f.read

and then:

str.each_line do |line|
    /ruby/.match(line)

ruby prints out variable line and say its a RegexpError
'empty range in character class' as if it interprets line as a Regexp
What am I doing wrong? There are a lot of strange characters in variable line,
maybe that is the issue?

I'm not sure about the issue, but the usual idiom for this is:

File.foreach("filename.dat") do |line|
  line.match(/ruby/)
end

This way:
- The file handler is automatically closed for you, you don't have to
ensure that you call close on it, it's done for you.
- You only have one line at a time in memory, since that seems to be
your unit of data. If the file is huge it will make a difference in
the amount of memory you will consume at a time.

In any case, String#match calls Regexp#match, so calling match on one
or the other should make no difference. Can you show a small example
of a file that breaks?

Jesus.

···

On Mon, Jun 13, 2011 at 7:32 PM, m b <snert@hotmail.se> wrote:

Hi,
I new to Ruby. It's a lot of fun to learn..

if I do this:

f=open('filename.dat','r')
str=f.read

and then:

str.each_line do |line|
/ruby/.match(line)

ruby prints out variable line and say its a RegexpError
'empty range in character class' as if it interprets line as a Regexp
What am I doing wrong? There are a lot of strange characters in variable line,
maybe that is the issue?

> if I do this:
>
> f=open('filename.dat','r')
> str=f.read
>
> and then:
>
> str.each_line do |line|
> /ruby/.match(line)
>
> ruby prints out variable line and say its a RegexpError
> 'empty range in character class' as if it interprets line as a Regexp
> What am I doing wrong? There are a lot of strange characters in variable line,
> maybe that is the issue?

I'm not sure about the issue, but the usual idiom for this is:

File.foreach("filename.dat") do |line|
  line.match(/ruby/)
end

This way:
- The file handler is automatically closed for you, you don't have to
ensure that you call close on it, it's done for you.
- You only have one line at a time in memory, since that seems to be
your unit of data. If the file is huge it will make a difference in
the amount of memory you will consume at a time.

In any case, String#match calls Regexp#match, so calling match on one
or the other should make no difference. Can you show a small example
of a file that breaks?

Jesus.

Aha.. Thats a better way to do it.. File.foreach Thanx.

the string-variable line both starts and and ends with a '/' . Its a series of paths from a linux system hd

But its a String to be searched and not a Regexp

> > if I do this:
> >
> > f=open('filename.dat','r')
> > str=f.read
> >
> > and then:
> >
> > str.each_line do |line|
> > /ruby/.match(line)
> >
> > ruby prints out variable line and say its a RegexpError
> > 'empty range in character class' as if it interprets line as a Regexp
> > What am I doing wrong? There are a lot of strange characters in variable line,
> > maybe that is the issue?

I found out what I was doing the wrong way..

I'm reading the search pattern with readline
and I wrote it -->/ruby/
That didnt make it a Regexp.
Now I found that if I write it -->ruby
and then
pattern=Regexp.new(input_str) it works..

/Mix

> > if I do this:
> >
> > f=open('filename.dat','r')
> > str=f.read
> >
> > and then:
> >
> > str.each_line do |line|
> > /ruby/.match(line)
> >
> > ruby prints out variable line and say its a RegexpError
> > 'empty range in character class' as if it interprets line as a Regexp
> > What am I doing wrong? There are a lot of strange characters in variable line,
> > maybe that is the issue?

You must have a different regular expression in your test other than
/ruby/ because that is valid:

irb(main):001:0> m = /ruby/.match "text"
=> nil
irb(main):002:0> m = /ruby/.match "ruby"
=> #<MatchData "ruby">

I found out what I was doing the wrong way..

I'm reading the search pattern with readline
and I wrote it -->/ruby/
That didnt make it a Regexp.

This is not true. The sequence /ruby/ *is* a valid regular expression.

10:01:30 ~$ ruby19 -e 'p /ruby/'
/ruby/
10:01:38 ~$ ruby19 -e 'p /ruby/.class'
Regexp
10:01:42 ~$

There must be something else going on. Please post your _complete_
code you actually have.

Kind regards

robert

···

On Mon, Jun 13, 2011 at 10:26 PM, m b <snert@hotmail.se> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/