Separation and merging is another way:
s = " abc " s.split(' ').join(' AND ') # => "a AND b AND c"
This has the advantage that ignoring spaces in leading and trailing spaces that Peter RE does not support:
s = " abc " s.gsub /\s+/, ' AND '
Delete spaces
s.split(' ').join('') # or s.delete(' ') # only deletes space chars
source share