Solved

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

Posted on 2010-08-12
3
442 Views
Last Modified: 2012-05-10
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
Comment
Question by:TIMFOX123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Accepted Solution

by:
wesgarrison earned 300 total points
ID: 33426061
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
 
LVL 12

Assisted Solution

by:cminear
cminear earned 200 total points
ID: 33431193
Very similar but more straightforward for your case: try this.
puts line if line.include? 'free'

Open in new window

0
 

Author Closing Comment

by:TIMFOX123
ID: 33432822
this is great !!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question