From: Simon Schuster Date: 2007-08-17T11:45:54+09:00 Subject: Re: backticks ahhh, haha. well, the part that got me was it returning a "3" because I just couldn't find 3 instances of "bar" in there... then I realized, f-o-o-b. 0, 1, 2, 3. :P On 8/16/07, Logan Capaldo wrote: > On 8/16/07, Simon Schuster wrote: > > whoa, can you explain a little what's going on here? I tried it and > > changed it around in irb but I can't really figure out what's going > > on. > > > > #{} just happens to work more places than string literals: > > If it helps clear it up, /#{needle}/ is roughly equivalent to > > Regexp.new("#{needle}") which is sort of a silly use of interpolation, > so let's write it as: > Regexp.new(needle) > > So yeah, the string "bar" was used to construct the regexp /bar/. > > > > > > > For added fun, try string interpolation inside regular expressions! > > > > > > haystack = "foobarbaz" > > > needle = "bar" > > > > > > haystack =~ /#{needle}/ > > > => 3 > > > > > > yeah :D > > > > > > > > > > > >