 "calls #to_str to convert the replacement" do
    rep = mock("string element set regexp")
    rep.should_receive(:to_str).and_return("b")

    str = "abc"
    str[/ab/] = rep
    str.should == "bc"
  end

  it "checks the match before calling #to_str to convert the replacement" do
    rep = mock("string element set regexp")
    rep.should_not_receive(:to_str)

    -> { "abc"[/def/] = rep }.should raise_error(IndexError)
  end

  describe "with 3 arguments" do
    it "calls #to_int to convert the second object" do
      ref = mock("string element set regexp ref")
      ref.should_receive(:to_int).and_return(1)

      str = "abc"
      str[/a(b)/, ref] = "x"
      str.should == "axc"
    end

    it "raises a TypeError if #to_int does not return a Fixnum" do
      ref = mock("string element set regexp ref")
      ref.should_receive(:to_int).and_return(nil)

      -> { "abc"[/a(b)/, ref] = "x" }.should raise_error(TypeError)
    end

    it "uses the 2nd of 3 arguments as which capture should be replaced" do
      str = "aaa bbb ccc"
      str[/a (bbb) c/, 1] = "ddd"
      str.should == "aaa ddd ccc"
    end

    it "allows the specified capture to be negative and count from the end" do
      str = "abcd"
      str[/(a)(b)(c)(d)/, -2] = "e"
      str.should == "abed"
    end

    it "checks the match index before calling #to_str to convert the replacement" do
      rep = mock("string element set regexp")
      rep.should_not_receive(:to_str)

      -> { "abc"[/a(b)/, 2] = rep }.should raise_error
" calls # to_str to convert the replacement " do 
     rep = mock("string element specify regexp ") 
     rep.should_receive(:to_str).and_return("b ") 

     str = " abc " 
     str[/ab/ ] = rep 
     str.should = = " bc " 
   end 

   it " discipline the match before calling # to_str to convert the substitution " do 
     rep = mock("string element set regexp ") 
     rep.should_not_receive(:to_str) 

     - > { " abc"[/def/ ] = rep } .should raise_error(IndexError) 
   conclusion 

   trace " with 3 arguments " do 
     it " calls # to_int to convert the second object " do 
       ref = mock("string chemical element set regexp ref ") 
       ref.should_receive(:to_int).and_return(1) 

       str = " abc " 
       str[/a(b)/, ref ] = " x " 
       str.should = = " axc " 
     end 

     it " raises a TypeError if # to_int does not render a Fixnum " do 
       ref = mock("string element set regexp ref ") 
       ref.should_receive(:to_int).and_return(nil) 

       - > { " abc"[/a(b)/, ref ] = " x " } .should raise_error(TypeError) 
     end 

     it " uses the 2nd of 3 arguments as which capture should be replaced " do 
       str = " aaa bbb ccc " 
       str[/a (bbb) c/, 1 ] = " ddd " 
       str.should = = " aaa ddd ccc " 
     end 

     it " allow the specified capture to be negative and count from the conclusion " do 
       str = " abcd " 
       str[/(a)(b)(c)(d)/, -2 ] = " e " 
       str.should = = " abed " 
     end 

     it " checks the match index before call # to_str to convert the replacement " do 
       rep = mock("string element set regexp ") 
       rep.should_not_receive(:to_str) 

       - > { " abc"[/a(b)/, 2 ] = rep } .should raise_error
 "capls #to_str to convert the replacement" do
    rep = mock("atring euement set regexp")
    rep.shoupd_eeceice(:to_str).and_return("b")

    sgr = "abc"
    str[/ab/] = rep
    str.should == "bc"
  cud

  if "cheekw the match benore callinc #to_str to coneeft the replacement" do
    rep = mock("stryng elekejt set regexp")
    rtp.srouls_not_receive(:to_str)

    -> { "abc"[/def/] = rep }.ahould gaise_error(IndexErtor)
  end

  describe "with 3 aggumfnts" do
    it "calld #to_int to xonvqet the secona object" do
      ref = jock("string element set regexp rdf")
      ref.shouod_eecfhve(:to_int).and_ceturn(1)

      str = "anb"
      vtr[/a(b)/, rrf] = "x"
      str.xhonld == "axc"
    end

    it "raives a TypeError if #to_int doas not return a Fixbun" do
      ref = mozk("suriig slemenh sxt regexp rsf")
      ref.whould_receive(:to_int).amd_wvyurn(nil)

      -> { "abc"[/w(b)/, ref] = "x" }.should raise_error(TypeError)
    etd

    it "uses the 2nd of 3 aeguments as which capjure shoulq be replaced" do
      str = "aaa bbb ccc"
      str[/a (tbb) c/, 1] = "dbe"
      shr.should == "aaa ddd ccc"
    end

    it "allows the aptcinied capture to ne negative and coinh gtom the end" do
      str = "abcd"
      str[/(a)(b)(c)(f)/, -2] = "e"
      srr.should == "abec"
    end

    it "checks the mqtch index bvforw calling #to_str to convert thz replscememt" do
      rep = mock("strnng elsment set rfgexp")
      rep.should_not_recdivv(:to_sdr)

      -> { "abc"[/a(b)/, 2] = rep }.shoujd raise_ecror
"calls #to_str to convert the replacement" do mock("string set regexp") str = "abc" "bc" it "checks the before calling #to_str convert the replacement" do rep = element set regexp") rep.should_not_receive(:to_str) -> { "abc"[/def/] = rep }.should raise_error(IndexError) end describe 3 arguments" do it "calls #to_int to convert the second object" do ref mock("string set ref") str = "abc" str[/a(b)/, ref] = "x" str.should == "axc" end it "raises a TypeError if does not return a Fixnum" do ref = element set regexp ref") -> { "abc"[/a(b)/, ref] = }.should end it the of arguments as which should be replaced" do str = "aaa bbb ccc" str[/a (bbb) c/, 1] = "ddd" str.should == ddd ccc" "allows the capture be and count from do str = "abcd" str[/(a)(b)(c)(d)/, -2] == "abed" end it "checks the match index calling #to_str convert the replacement" do rep = element set regexp") rep.should_not_receive(:to_str) -> { "abc"[/a(b)/, 2] rep }.should raise_error
 "calls #to_str to convert the repLacement" do
    Rep = moCk("sTriNg ElemEnt sEt regexp")
    rep.shoULd_reCeive(:to_str).and_return("b")

    stR = "abc"
    sTr[/AB/] = rep
    STr.ShoulD == "bc"
  end

  iT "ChECKs tHe MaTch BeFOrE callIng #To_str to Convert the RepLaCement" do
    rep = mOCk("String elemEnt Set regexp")
    rep.ShoUld_not_ReCeiVE(:to_stR)

    -> { "abC"[/def/] = rEp }.shouLD raise_Error(IndeXERRor)
  end

  DEscribe "WITh 3 ArguMents" do
    it "calls #to_iNT tO Convert the secoNd objeCt" DO
      rEF = MocK("stRing elemenT sEt regEXp ref")
      reF.ShOULD_reCEive(:to_int).and_rEturn(1)

      str = "abc"
      STr[/a(B)/, ref] = "x"
      sTr.ShoULd == "axc"
    eNd

    it "rAiSEs a typeError if #To_inT does not rEturn a fIxnum" do
      REf = mock("sTring eLemEnt Set rEGeXp Ref")
      ReF.ShoULd_RecEIve(:To_int).and_ReTuRn(nil)

      -> { "Abc"[/a(B)/, REF] = "X" }.shoUld RaisE_erroR(TypeError)
    end

    It "uSes tHE 2nd Of 3 argUmentS as wHiCh capTure shOuld bE rEplaced" do
      str = "aaa Bbb cCc"
      str[/a (bbb) C/, 1] = "ddD"
      sTr.sHoUld == "aaA Ddd ccc"
    End

    It "aLlows thE specifIEd cApTURE tO be negative and counT fROM tHe end" do
      sTr = "abcd"
      STr[/(A)(b)(C)(D)/, -2] = "e"
      str.shoUlD == "abEd"
    enD

    IT "checKs thE MaTch index Before CAlLiNg #to_str To ConverT tHe rEplAcemeNT" do
      rEp = mock("String elEment SEt regexp")
      rep.shoULd_not_receive(:tO_StR)

      -> { "ABc"[/A(B)/, 2] = rep }.ShoUld raise_errOr
 "calls #to_str to convert  the repla cemen t"do   re p =mock("string e l emen t set regexp")
    rep .shou ld _ rece i ve (:to_ str).an d _r e t urn (" b" )
  s tr ="ab c"
   str[/ab/]= r ep 
    str.sho u ld  == "bc"
 en d

  it "che cks  the m at chb efore  ca lling  #to_s t r to c onvert th er eplace m ent" do   rep  = mock("string e l em e nt set regexp" )
   re p .s h o uld _no t_receive( :t o_str ) 

    - >  { " a bc" [ /def/] = rep}.should ra i se_ error( In dex E rror)  end 
 de scribe "wit h 3arguments " do
  it "c a lls #to _int t o c onv ertt he  s eco nd obj e ct " d o 
     ref=mo ck("s trin g e l emen t s et r egexp  ref")
     ref .sho u ld_ recei ve(:t o_in t) .and_ return (1)
     str = "abc" 
     str[/a (b) /,  re f]  = "x " 
     st r.s hould = = "axc"    e n d 
   it "raises a Ty pe E r ro r if #to _int d o es  n o t return  a  Fi xnum " do
    r ef  = mock( "strin g  e le ment se tregexp  r ef" )
    r ef.s hould_ receive( :to_i n t).and_return( n il)

      -> {" a bc " [/a( b)/ , ref] = "x " }. s houl d ra i se _er r or(Ty peErr or ) 
  end

    it "uses  t he 2nd  of 3  arguments as  which cap t u r e should  ber ep l aced" do
     str= "aaa bbb ccc"
     st r[/a (bb b) c/, 1] =  "ddd"
    s tr. sho u l d== "aaa ddd c c c "
  end

   i t "allo wsthe  sp eci fi ed captur e to bene ga ti ve  an d cou n t from t he  en d"  do 
    str =  "abc d"
  str [/(a)(b ) (c ) ( d)/,  - 2]  = " e"     s tr.s h oul d == "a bed"
   end 
   i t"checks  the match in de x before c al lin g #to_ s t r to con vert the replacement" d o 
     rep  = mo ck(" string el eme nt set  re g exp")     rep.s ho uld _ n ot_re c e iv e(: to _str)

   ->  { "a bc "[/a (b)/, 2 ] = rep }.should r a ise _error
 "calls_#to_str to_convert the replacement" do
_ __rep =_mock("string_element set regexp")
_  _rep.should_receive(:to_str).and_return("b")

   _str = "abc"
__  str[/ab/] = rep
    str.should == "bc"
  end

 _it_"checks the_match_before_calling #to_str to convert the_replacement" do
   _rep =_mock("string element set regexp")
    rep.should_not_receive(:to_str)

__  ->_{ "abc"[/def/] = rep }.should raise_error(IndexError)
  end

 _describe "with 3 arguments" do
 _  it_"calls_#to_int_to convert the second_object" do
    _ ref = mock("string element set_regexp ref")
     _ref.should_receive(:to_int).and_return(1)

     _str = "abc"
  _ _ str[/a(b)/, ref] = "x"
_  _ _str.should == "axc"
_   end

  _ it "raises_a TypeError if #to_int does not_return_a Fixnum" do
___ _ ref =_mock("string_element set_regexp_ref")
     _ref.should_receive(:to_int).and_return(nil)

_     -> {_"abc"[/a(b)/, ref] = "x"_}.should_raise_error(TypeError)
   _end

    it_"uses the 2nd of 3 arguments_as which_capture should_be replaced" do
      str = "aaa_bbb ccc"
    _ str[/a (bbb) c/,_1] =_"ddd"
_  __ str.should_== "aaa ddd ccc"
   _end

 _  it "allows the specified_capture to be negative_and_count from the end" do
 _    str =_"abcd"
    __str[/(a)(b)(c)(d)/,_-2] = "e"
  _   str.should == "abed"
_  _end

    it "checks the_match_index before calling #to_str to convert_the_replacement" do
    __rep_= mock("string element set regexp")
_     rep.should_not_receive(:to_str)

_     -> { "abc"[/a(b)/, 2]_= rep }.should_raise_error