# Beginner's q about eval and/or code blocks

**URL:** <https://www.ruby-forum.com/t/beginners-q-about-eval-and-or-code-blocks/194984>\
**Category:** Ruby\
**Created:** [September 14, 2010, 9:16pm UTC](https://www.ruby-forum.com/t/beginners-q-about-eval-and-or-code-blocks/194984 "2010-09-14T21:16:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [September 14, 2010, 9:16pm UTC](https://www.ruby-forum.com/t/beginners-q-about-eval-and-or-code-blocks/194984/1 "2010-09-14T21:16:34Z")

</div>

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.

I’m positive this is easy in Ruby but currently it’s driving me nuts.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [September 14, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/beginners-q-about-eval-and-or-code-blocks/194984/2 "2010-09-14T22:00:23Z")

</div>

Brian C. 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.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [September 14, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/beginners-q-about-eval-and-or-code-blocks/194984/3 "2010-09-14T21:20:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [September 14, 2010, 10:06pm UTC](https://www.ruby-forum.com/t/beginners-q-about-eval-and-or-code-blocks/194984/4 "2010-09-14T22:06:37Z")

</div>

Brian C. 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?

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [September 14, 2010, 11:12pm UTC](https://www.ruby-forum.com/t/beginners-q-about-eval-and-or-code-blocks/194984/5 "2010-09-14T23:12:23Z")

</div>

Jeff Myers wrote:

> 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?

No. It’s because you’re trying to do nil.chomp. Possibly, $stdin.gets is  
returning nil for end-of-file. You need to post your actual code if you  
want an accurate answer, otherwise I have to guess.

(The “private method” error is confusing; just pretend it said “no such  
method”. It’s because there’s also a standalone ‘chomp’ method defined  
in the Kernel module. This is a legacy Perlism, and serves little  
purpose except to confuse newcomers 🙂

Better to test for nil before you chomp. Example:

loop do  
print "Enter a pattern: "  
line = $stdin.gets  
break if line.nil?  
line.chomp!  
re = Regexp.new(line)  
… rest of logic goes here  
end
