100 Hours of Testing Practice - 32 of 100 (Bug fix... woops)

Rain, rain, rain... and a weekend in. Which works well as I get more hours to practice a bit of Ruby.

In the post on data from text files I noticed there was a call to a method file_search that wasn't in the attached file, cool... The reason was that I couldn't get it to work and so took it out.

The idea you'll recall was to read in a simple .txt file and do-some-stuff with the data. The idea of file_search was that we'd read the data file in and search for a given string. If we were working with data files then avoiding duplicates or modifying them in some way could be pretty important.

.

Which operator to use?

Our method for searching for the string looks like this:

All the things we've seen before, puts some messages, gets.chomp a response from the user, then open a file ready to work with, finally read the file contents into our text variable. The next thing will be to search for the user entered search_term and see if it matches a string from the file.

Here's the if... statement I came up with at first:

The problem with this is it doesn't work as the =~ operator causes an error to be returned, namely (TypeError). Looking over the Ruby docs it turns out this operator isn't useable for what we're trying to do. We know that = isn't what we need as that's for assignment and == won't work as that's for testing equality, though it seemed like a good choice at first.

A different approach

Various attempts at getting an if... statement to work didn't. I'm guessing there is a way to make it work but it wasn't revealing itself to me. Instead I opted for the idea that bubbled up when I was lay in bed day-dreaming about the problem, just before I woke.o_O

Having played with the short version of the if statement, where it's just on one line, I recalled the method contains. My idea was this could be used to ask if the text file contains the search string from the user. Playing with it however did not work and of course I can't find any reference in the Ruby docs (probably because I don't know where to look/search).

A Google around for ideas turned up a slightly cryptic post on the Ruby Forums, where include? was suggested. Playing around with the short form if... statement and the used of include? to get the right value produced this line:

.

As always the solution is elegantly simple, when you know how... Here we ask 'does the working_file include the search_term the user entered? The result will be true or false. Therefore the outer set of brackets will then contain that value and so if it's true we get "We have that one", when false we get the second message.

.

I'm getting used to stringing methods and values together. Here we have 4 or 5 depending on how you look at it. A few weeks ago I'd have been trying to go through each step, well... step by step. It's good to start wiring them together like this and begin creeping slowly away from a 'batch scripting' mentality. In the process I'm also reading the Docs and seeing what other things do, looking at say ||= and =~ for example. I'll forget a lot I'm sure, but they wont be so unfamiliar next time they're encountered.

.

Code for the corrected method in the attached externalFile2.rb

.

In the UK for a couple of days next week so will try to get some study in on the plane and train.

Views: 205

Tags: 100hours, Ruby

Add a Comment

You need to be a member of Software Testing Club - An Online Software Testing Community to add comments!

Join Software Testing Club - An Online Software Testing Community

Comment by Mark Crowther on February 23, 2012 at 11:39

Thanks for the links Phil - I was thinking this needed a little Regex sneaking in but decided to avoid it for now. I appreciate you doing the digging :)

 first_string = "banana"
 second_string = "I like a banana with my breakfast"
 puts "We have banana!!" if second_string =~ /#{Regexp.quote(first_string)}/


"I'm finding this useful practice seeing if I can find out why things aren't working for you"

lol, yer gonna be busy then :p


STC TEAM
Comment by phil kirkham on February 21, 2012 at 8:57

Hi Mark,

 

To get your original to work, try something like this

~/#{ search_term}/

 

see this Stackexchange thread

http://stackoverflow.com/questions/150095/ruby-regex-string-insertion

 

and read up on the regex object here

http://www.regular-expressions.info/ruby.html

 

I'm finding this useful practice seeing if I can find out why things aren't working for you

Adverts

Ministry of Testing

© 2014   Created by Rosie Sherry.

Badges  |  Report an Issue  |  Terms of Service