Puzzled on the MatchData

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

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

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