Sha256: 6e1f6947ae2db05edbdb4792340a35f544357c276fb4edf8bec4702198b63c80

Contents?: true

Size: 999 Bytes

Versions: 135

Compression:

Stored size: 999 Bytes

Contents

class String
  def indent(n)
    if n >= 0
      gsub(/^/, ' ' * n)
    else
      gsub(/^ {0,#{-n}}/, "")
    end
  end
  
  # re.source.gsub(/\([^)]*\)/, '$var')
  # Cumulative #sub
  def subs(re, *args)
    args.inject(self) do |s,arg|
      s.sub(re, arg)
    end
  end

  # TODO: Use subs instead...
  def gzub(regexp, format=nil, &proc)
    md = match(regexp)
    raise "#{self.inspect} doesn't match #{regexp.inspect}" if md.nil?
    
    s = dup
    pos = 0
    md.captures.each_with_index do |m, n|
      replacement = if block_given?
        proc.call(m)
      elsif Proc === format
        format.call(m)
      else
        format % m
      end
      
      if md.offset(n+1)[0]
        s[md.offset(n+1)[0] + pos, m.length] = replacement
        pos += replacement.length - m.length
      end
    end
    s
  end

  if (Cucumber::JRUBY && Cucumber::RAILS) || Cucumber::RUBY_1_9
    # Workaround for http://tinyurl.com/55uu3u 
    alias jlength length
  else
    require 'jcode'
  end
end

Version data entries

135 entries across 135 versions & 10 rubygems

Version Path
aslakhellesoy-cucumber-0.1.100.1 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.100.2 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.100.3 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.100.4 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.100.5 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.16.5 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.1 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.10 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.11 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.12 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.13 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.14 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.15 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.17 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.18 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.19 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.2 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.20 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.21 lib/cucumber/core_ext/string.rb
aslakhellesoy-cucumber-0.1.99.22 lib/cucumber/core_ext/string.rb