# Puzzled on the MatchData

**URL:** <https://www.ruby-forum.com/t/puzzled-on-the-matchdata/96709>\
**Category:** Ruby\
**Created:** [June 30, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/puzzled-on-the-matchdata/96709 "2006-06-30T23:54:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vcivelek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vcivelek](https://www.ruby-forum.com/u/vcivelek)\
**Post date:** [June 30, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/puzzled-on-the-matchdata/96709/1 "2006-06-30T23:54:45Z")

</div>

Hi,  
I have a question…

Why the below snippet,

[“foo”,“bar”,“baz”].each do |s|  
m = /^ba(.\*?)$/.match(s)  
puts m[0]  
end

gives the following error?

test.rb:8: undefined method `[]’ for nil:NilClass (NoMethodError)

Regards,  
Volkan

---

<div class="post-metadata">

**Author:** ![vcivelek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vcivelek](https://www.ruby-forum.com/u/vcivelek)\
**Post date:** [July 1, 2006, 12:21am UTC](https://www.ruby-forum.com/t/puzzled-on-the-matchdata/96709/2 "2006-07-01T00:21:38Z")

</div>

Volkan Civelek wrote:

> Hi,  
> I have a question…
> 
> Why the below snippet,
> 
> [“foo”,“bar”,“baz”].each do |s|  
> m = /^ba(.\*?)$/.match(s)  
> puts m[0]  
> end
> 
> gives the following error?
> 
> test.rb:8: undefined method `[]’ for nil:NilClass (NoMethodError)
> 
> Regards,  
> Volkan

nevermind…  
i understood now…  
thanks  
answer:  
[“foo”,“bar”,“baz”].each do |s|  
m = /^ba(.\*?)$/.match(s) and puts m[0]  
end
