Sha256: 5fa4a5b7ea022a14662721f1de19b43ef3aba6d6549a4e66be29b0f2cb8483f5
Contents?: true
Size: 1.18 KB
Versions: 2
Compression:
Stored size: 1.18 KB
Contents
class String # Overwrite this method to provide your own translations. def self.translate(value) translations[value] || value end def self.translations @translations ||= {} end # Matches any whitespace (including newline) and replaces with a single space # # @example # <<QUERY.compress_lines # SELECT name # FROM users # QUERY # => "SELECT name FROM users" def compress_lines(spaced = true) split($/).map { |line| line.strip }.join(spaced ? ' ' : '') end # Useful for heredocs - removes whitespace margin. def margin(indicator = nil) lines = self.dup.split($/) min_margin = 0 lines.each do |line| if line =~ /^(\s+)/ && (min_margin == 0 || $1.size < min_margin) min_margin = $1.size end end lines.map { |line| line.sub(/^\s{#{min_margin}}/, '') }.join($/) end # Formats String for easy translation. Replaces an arbitrary number of # values using numeric identifier replacement. # # @example # "%s %s %s" % %w(one two three) #=> "one two three" # "%3$s %2$s %1$s" % %w(one two three) #=> "three two one" def t(*values) self.class::translate(self) % values end end # class String
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
extlib-0.9.3 | lib/extlib/string.rb |
extlib-0.9.2 | lib/extlib/string.rb |