We're going to need some sanity checking and some fixing up here. How about:

```ruby
def extract(data)
  lines = data.lines

  if lines.any? && (match = lines[0].match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/
    bang.sub!(/^#! /, '#!')
    tokens = bang.split(' ')
    pieces = tokens.first.split('/')

    if pieces.size > 1
      script = pieces.last
    else
      script = pieces.first.sub('#!', '')
    end

    script = script == 'env' ? tokens[1] : script

    # "python2.6" -> "python"
    if script =~ /((?:\d+\.?)+)/
      script.sub! $1, ''
    end

    script
  end
end
```