From: Jeff Myers Date: 2010-09-15T05:00:25+09:00 Subject: Re: beginner's q about eval and/or code blocks Brian Candler wrote: > Jeff Myers wrote: >> This ruby program successfully prints all 5-letter words starting with >> 'j' >> (in a text file containing all current U.S. English Scrabble words). >> >> fp = File.open('C:\Documents and Settings\User\twl06FromMac.txt') >> fp.each do |line| >> puts line if line =~ /^j....$/ >> end >> >> I would like to allow the user to enter the regex rather than have it >> hard-coded as above. I've tried to get eval to work after building a >> string to pass it. No luck so far. > > Try something like: > > str = $stdin.gets.chomp > re = Regexp.new(str) > ... > puts line if line =~ re Excellent! Thanks. Now I'll spend some time digging into the Why of it all. Apparently the match operator needs one side to be a regular expression object. -- Posted via http://www.ruby-forum.com/.