Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

In ruby, how do I just select lines in a file with the string "free" in them.

I have a data  file:
vette  20K
honda  free
kia  5K

My program:
# ruby file processing, version 1
file='C:\Documents and Settings\My Documents\rubyme.txt'
f = File.open(file, "r")
f.each_line { |line|
      puts line.grep("free")
}
f.close

I made up the command grep but you know what I want.  I just want the lines of the datafile with "free" in them.  
0
TIMFOX123
Asked:
TIMFOX123
2 Solutions
 
wesgarrisonCommented:
Use a regex with =~ to match.
No match will return nil, which is false-y.
puts line if line =~ /free/

Open in new window

0
 
cminearCommented:
Very similar but more straightforward for your case: try this.
puts line if line.include? 'free'

Open in new window

0
 
TIMFOX123Author Commented:
this is great !!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now