On 16 Nov 2007, at 15:49, Feng Tien wrote:

> I know grep needs to take an regular expression.
>
> Is there anyway to have it take a regular string instead? without  
> using
> regular expressions?
> -- 

What grep are you referring to? Enumerable#grep doesn't need an RE. A  
String works fine as does any === implementing object:

-------------------------------------------------------- Enumerable#grep
      enum.grep(pattern)                   => array
      enum.grep(pattern) {| obj | block }  => array
------------------------------------------------------------------------
      Returns an array of every element in _enum_ for which +Pattern ===
      element+. If the optional _block_ is supplied, each matching
      element is passed to it, and the block's result is stored in the
      output array.

         (1..100).grep 38..44   #=> [38, 39, 40, 41, 42, 43, 44]
         c = IO.constants
         c.grep(/SEEK/)         #=> ["SEEK_END", "SEEK_SET", "SEEK_CUR"]
         res = c.grep(/SEEK/) {|v| IO.const_get(v) }
         res                    #=> [2, 0, 1]

[alexg / powerbook]/Users/alexg/Desktop(7): irb
irb(main):001:0> ['a','b','c'].grep('a')
=> ["a"]

Or do you mean you want use a String as if it were an RE. If so then  
you can just make one from the String:

irb(main):003:0> ['foo','bar','foobar'].grep(Regexp.new('foo'))
=> ["foo", "foobar"]

Alex Gutteridge

Bioinformatics Center
Kyoto University