[holy ruby programmers batman!] Stupid string tricks

Avdi Grimm avdi at avdi.org
Tue Dec 18 17:51:05 EST 2007


I just added this to my .irbrc:

# Add the unix-style pipe command to strings
class String
  def |(cmd)
    IO.popen(cmd, 'r+') do |pipe|
      pipe.write(self)
      pipe.close_write
      pipe.read
    end
  end
end

Usage:

    "foo\nbar\nbaz" | 'sort' # => "bar\nbaz\nfoo"

Feel free to nab it for Utility Belt if you think others might find it useful.

-- 
Avdi


More information about the Utilitybelt-tinkering mailing list