# Copyright (C) 2010 Rocky Bernstein class Trepan module Util def safe_repr(str, max, elipsis='... ') if str.is_a?(String) && str.size > max && !str.index("\n") "%s%s%s" % [ str[0...max/2], elipsis, str[str.size-max/2..str.size]] else str end end module_function :safe_repr end end if __FILE__ == $0 include Trepan::Util string = 'The time has come to talk of many things.' puts safe_repr(string, 50) puts safe_repr(string, 17) puts safe_repr(string.inspect, 17) puts safe_repr(string.inspect, 17, '') end