Sha256: 0789cdb7b1f23ff161f1856b3bdba0e7fc1d332fa6b44e8fb01f6adef03fdfee

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

class String
  def self.random_letters count, type = :lower
   letters(type).pick(count)
  end

  def self.letters type = :lower
    letters = ('a'..'z').to_a
    type == :upper ? letters.map!(&:upcase) : letters
  end

  unless respond_to? :trim
    def trim
      self.strip
    end
  end
 
  unless respond_to? :concat
    def concat *args
      args.inject(self) do |res, arg| 
        res << arg.to_s
        res
      end
    end
  end

  # remove prefixed '-' signs, then allow any letter, number, underscore '_' or dash '-'
  def alpha_numeric
    self.gsub(/^\-+/, '').gsub(/[^0-9a-zA-Z_\-]+/i, '')
  end

  def insert_before_last str, marker = 'end'
    res = []
    found = false
    marker = case marker
    when Symbol, String
      marker.to_s
    when Hash
      marker[:marker].to_s
    else
      raise ArgumentException, "last argument is the marker and must be a String, Symbol or even Hash with a :marker option pointing to the marker (String or Symbol)"
    end

    marker = Regexp.escape(marker.to_s.reverse)
    nl = Regexp.escape("\n")
    self.reverse.each_line do |x|
      x.gsub! /#{nl}/, ''
      if !found && x =~ /#{marker}/
        replace = "#{str}\n" << x.reverse
        res << replace
        found = true
      else
        res << x.reverse
      end
    end
    res = res.reverse.join("\n")
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sugar-high-0.5.1 lib/sugar-high/string.rb