From: Robert Klemme Date: 2012-06-08T20:42:07+09:00 Subject: Re: Best way to partition a string On Fri, Jun 8, 2012 at 12:20 PM, Iņaki Baz Castillo wrote: > 2012/6/8 Jan E. : >> "Iņaki Baz Castillo" wrote in post #1063634: >>> 2012/6/7 cyber c. : >>> Regexp.new(/^([a-zA-Z]+)(.+)$/).match(str).captures >> >> Why are you using Regexp.new, when /^([a-zA-Z]+)(.+)$/ already *is* a >> Regexp? This seems nonsensical to me, like String.new("abc"). > > Yes, I'm used to store my regexps in some module/class constants and > usually use Regexp.new for some reason I don't remember right now, > maybe because it allows passing a string which is useful in certain > cases: > > Regexp.new("^([a-zA-Z]+)(.+)$") Like irb(main):001:0> s = "foo" => "foo" irb(main):002:0> rx = /^([a-zA-Z]+)#{s}(.+)$/ => /^([a-zA-Z]+)foo(.+)$/ irb(main):003:0> puts rx (?-mix:^([a-zA-Z]+)foo(.+)$) => nil ? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/