Tuesday, 15 February 2011

Ruby easy way to assign fixed value if a regex dont match? -



Ruby easy way to assign fixed value if a regex dont match? -

i have this:

author = m.match(/author: (\w*)/)[1].strip

it throw me "undefined method []" exception

what best way assign fixed value (for illustration "" or "not found") if regex dont match? maybe rescue?

the best way is:

author = m[/regex/, 1] || "not found"

ruby incredible.

ruby

No comments:

Post a Comment