Correct use of =~ to match a string in a file?

Hiya All,

Back again with more noobie questions ;}

I’ve been playing with some basics of opening and writing to files. But
have come up stuck trying to search inside a given file.

I have small method for matching a search string entered by a user,
against one of three in a .txt file. The method is available here:

http://codepad.org/DWIURKDV

I know I’m missing something fundamental but I can’t pin it down. An
error is produced that says “Type mismatch: string given” for the =~

However, I used this in a simpler version as below and it works:


first_string = “banana”
second_string = “I like a banana with my breakfast”

puts “\nWe have banana!!” if second_string =~
/#{Regexp.quote(first_string)}/

I’ve tried all sorts and can’t get the method linked above to work.

If anyone has a few moments could you look over this for me?

Out of interest, other examples I’ve worked on are at:
https://github.com/MarkCTest/Script-Bucket/blob/master/externalFile.rb

Thanks as always,

DE

@Dave E.

Your code runs fine on my machine,

Well what the heck… it works fine for me too o_O

I have an old version on my system and can’t now find the one I pasted.
Obviously need to learn a bit of version controlling along with my Ruby
:slight_smile:

Apologies, appreciate you looking at this though!

DE

hey Dave,

your code at http://codepad.org/DWIURKDV also works fine for me…
what are you entering as the search term at the command line? maybe
that’s the problem?

  • j

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs