Sha256: 1c8fcbaadf79391be7f4e6a1f73c32734fd94a17249984e91a4f4c95d600c45b

Contents?: true

Size: 852 Bytes

Versions: 2

Compression:

Stored size: 852 Bytes

Contents

class String
  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.reverse)
    nl = Regexp.escape("\n")
    # puts self
    # puts "marker: #{marker}"
    # puts "nl: #{nl}"    
    # puts "str: #{str}"    
  
    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

2 entries across 2 versions & 1 rubygems

Version Path
sugar-high-0.3.3 lib/sugar-high/string.rb
sugar-high-0.3.2 lib/sugar-high/string.rb