Changeset 1085

Show
Ignore:
Timestamp:
12/12/07 19:28:14 (1 year ago)
Author:
e.@brainspl.at
Message:

refactor String#indent, closes #357 [pythonic]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/core_ext/string.rb

    r1046 r1085  
    4949  #   #=> "   Hello\n   Bob\n   How are you?" 
    5050  def indent(indentation) 
    51     lines = to_a 
    52     initial_indentation = lines.first.scan(/^(\s+)/).flatten.first 
    53     lines.map do |line| 
    54       if initial_indentation.nil? 
    55         " " * indentation + line 
    56       elsif line.index(initial_indentation) == 0 
    57         " " * indentation + line[initial_indentation.size..-1] 
    58       else 
    59         " " * indentation + line 
    60       end 
    61     end.join 
     51    match(/\s*/) && gsub(/^\s{0,#{$&.length}}/, " " * indentation) 
    6252  end 
    63    
    6453end 
  • trunk/lib/merb/server.rb

    r1070 r1085  
    179179          options[:generate] = File.expand_path(argv.last) 
    180180        end 
    181          
     181                
    182182        # Load up the configuration from file, but keep the command line 
    183183        # options that may have been chosen. Also, pass-through if we have 
  • trunk/spec/merb/core_ext_spec.rb

    r1021 r1085  
    402402    end.should_not raise_error(Timeout::Error) 
    403403  end 
     404 
     405  it "should remove any indentation and add +indentation+ number of spaces" do 
     406    "foo\n  bar\n".indent(3).should == "   foo\n     bar\n" 
     407    "  foo\n    bar\n".indent(3).should == "   foo\n     bar\n" 
     408  end 
    404409end 
    405410