Solved

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

Posted on 2010-08-12
3
441 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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