From: Jeff Myers Date: 2010-09-15T05:06:38+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 If I put the user input into a simple loop, it has trouble: "private method 'chomp' called for nil:NilClass (NoMethodError). Is there a problem with redefining re? -- Posted via http://www.ruby-forum.com/.