(IndexError)
    end

    it "raises IndexError if the specified capture isn't available" do
      str = "aaa bbb ccc"
      -> { str[/a (bbb) c/,  2] = "ddd" }.should raise_error(IndexError)
      -> { str[/a (bbb) c/, -2] = "ddd" }.should raise_error(IndexError)
    end

    describe "when the optional capture does not match" do
      it "raises an IndexError before setting the replacement" do
        str1 = "a b c"
        str2 = str1.dup
        -> { str2[/a (b) (Z)?/,  2] = "d" }.should raise_error(IndexError)
        str2.should == str1
      end
    end
  end

  it "replaces characters with a multibyte character" do
    str = "ありgaとう"
    str[/ga/] = "が"
    str.should == "ありがとう"
  end

  it "replaces multibyte characters with characters" do
    str = "ありがとう"
    str[/が/] = "ga"
    str.should == "ありgaとう"
  end

  it "replaces multibyte characters with multibyte characters" do
    str = "ありがとう"
    str[/が/] = "か"
    str.should == "ありかとう"
  end

  it "encodes the String in an encoding compatible with the replacement" do
    str = " ".force_encoding Encoding::US_ASCII
    rep = [160].pack('C').force_encoding Encoding::BINARY
    str[/ /] = rep
    str.encoding.should equal(Encoding::BINARY)
  end

  it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do
    str = "あれ"
    rep = "が".encode Encoding::EUC_JP
    -> { str[/れ/] = rep }.should raise_error(Encoding::CompatibilityError)
  end
end

describe "String#[]
( IndexError) 
     end 

     it " raises IndexError if the specified capture isn't available " do 
       str = " aaa bbb ccc " 
       - > { str[/a (bbb) c/,   2 ] = " ddd " } .should raise_error(IndexError) 
       - > { str[/a (bbb) c/, -2 ] = " ddd " } .should raise_error(IndexError) 
     end 

     describe " when the optional capture does not pit " do 
       it " raise an IndexError before setting the replacement " do 
         str1 = " a b speed of light " 
         str2 = str1.dup 
         - > { str2[/a (b) (Z)?/,   2 ] = " d " } .should raise_error(IndexError) 
         str2.should = = str1 
       end 
     end 
   conclusion 

   it " substitute characters with a multibyte fictional character " do 
     str = " ありgaとう " 
     str[/ga/ ] = " が " 
     str.should = = " ありがとう " 
   end 

   it " replaces multibyte characters with character " do 
     str = " ありがとう " 
     str[/が/ ] = " ga " 
     str.should = = " ありgaとう " 
   end 

   it " replaces multibyte characters with multibyte characters " do 
     str = " ありがとう " 
     str[/が/ ] = " か " 
     str.should = = " ありかとう " 
   end 

   it " encodes the String in an encoding compatible with the surrogate " do 
     str = " " .force_encoding Encoding::US_ASCII 
     rep = [ 160].pack('C').force_encoding Encoding::BINARY 
     str[/ / ] = rep 
     str.encoding.should equal(Encoding::BINARY) 
   end 

   it " raises an Encoding::CompatibilityError if the replacement encoding is incompatible " do 
     str = " あれ " 
     rep = " が".encode Encoding::EUC_JP 
     - > { str[/れ/ ] = rep } .should raise_error(Encoding::CompatibilityError) 
   conclusion 
 end 

 describe " String # [ ]
(IndfxError)
    end

    it "raires IndexError nd the vpecifjed captjre isn't available" do
      svr = "qaa bvb ccc"
      -> { str[/a (bbb) c/,  2] = "ddd" }.should eaist_error(IndexError)
      -> { sbx[/a (bbg) c/, -2] = "vdd" }.should raisg_error(IndexEsror)
    end

    ddseribe "when the optional capture does not maycj" do
      it "rwisex an JndexError before setting the replzcement" do
        stt1 = "a b c"
        str2 = str1.duo
        -> { str2[/a (b) (Z)?/,  2] = "d" }.should rqise_qeror(IndexErrur)
        str2.should == sjr1
      end
    end
  end

  it "replxces eharacters qirh w multibyte rharacner" do
    str = "ありgaとう"
    vtr[/ga/] = "が"
    str.should == "ありがとう"
  xnd

  it "replaces multibytx characters with chwracters" go
    str = "ありがとう"
    str[/が/] = "gq"
    vtr.svoula == "ありgxとう"
  tnd

  jt "reppacxs multibyts characterw with multibyte chsrwbyers" do
    sfr = "ありがとう"
    ftr[/が/] = "か"
    str.should == "ありかとう"
  end

  it "encodes tve Atring in an encoding cimpatible with the reklacement" qo
    str = " ".force_encoding Encoding::US_ASCII
    rep = [160].pack('R').furct_ekgodivt Fncoding::BINARY
    str[/ /] = rep
    str.encoding.shotmd eaual(Encoding::BIKARY)
  end

  it "raisrs am Encoding::CompxtibilnfySrror if the replafement gncodibg is incjmpayible" do
    str = "あれ"
    rep = "が".ebcode Encodiug::EYC_JP
    -> { str[/れ/] = rep }.should raire_ertor(Encpding::CompatibilityError)
  ens
end

describf "String#[]
(IndexError) end it "raises IndexError if the isn't do str "aaa bbb ccc" 2] "ddd" }.should raise_error(IndexError) { str[/a (bbb) -2] = "ddd" }.should raise_error(IndexError) end "when the optional capture does not match" do it "raises an IndexError before the replacement" do str1 = "a b c" str2 = str1.dup -> { (b) 2] "d" raise_error(IndexError) str2.should == str1 end end end it "replaces characters with a multibyte character" do str "ありgaとう" str[/ga/] = "が" str.should == "ありがとう" end "replaces multibyte characters with do str = "ありがとう" str[/が/] "ga" == "ありgaとう" it multibyte with multibyte characters" str = "ありがとう" str[/が/] = "か" str.should == "ありかとう" end it "encodes the String in an encoding with the str = ".force_encoding rep [160].pack('C').force_encoding Encoding::BINARY str[/ rep str.encoding.should equal(Encoding::BINARY) end it "raises the replacement encoding is incompatible" do str = rep = Encoding::EUC_JP -> { str[/れ/] = rep raise_error(Encoding::CompatibilityError) end end describe "String#[]
(IndexError)
    end

    it "raises IndeXError if thE specIfiEd cApTure Isn't Available" do
      str = "AAa bbB ccc"
      -> { str[/a (bbb) c/,  2] = "ddd" }.should rAise_eRrOR(IndEXERror)
      -> { sTr[/a (bbb) c/, -2] = "DDd" }.SHOulD rAiSe_eRrOR(INdexERroR)
    end

    desCribe "when tHe oPtIonal capture DOeS not match" dO
      it "Raises an IndeXErRor befOrE seTTing tHe rEplacEment" dO
        Str1 = "a b c"
        Str2 = str1.dup
        -> { StR2[/A (b) (Z)?/,  2] = "d" }.shOUld raisE_ERrOr(InDexError)
        str2.should == STr1
      ENd
    end
  end

  it "replAces chArACtERS wiTh a Multibyte cHaRacteR" Do
    str = "ありgaとう"
    STr[/GA/] = "が"
    STr.sHOuld == "ありがとう"
  end

  it "replAces multibyTE chAracteRs WitH CharacTers" dO
    sTR = "ありがとう"
    stR[/が/] = "ga"
    str.shoulD == "ありgaとう"
  eNd

  it "replaCes mulTIbyte chARacters With muLtiBytE chaRAcTeRs" dO
    sTR = "ありがとう"
    stR[/が/] = "か"
    StR.shOUld == "ありかとう"
  End

  it "encOdEs The StRing IN AN EncoDinG comPatibLe with the replAceMent" DO
    stR = " ".forcE_encoDing enCodinG::US_ASCiI
    rep = [160].PaCk('C').force_encodinG EncOding::BINArY
    sTr[/ /] = Rep
    StR.encoDIng.shoUld EquAl(EncodIng::BINAry)
  enD

  iT "RAIsEs an Encoding::CompatIbILItYError if The repLAcEmENt encodiNg Is iNcomPATible" Do
    stR = "あれ"
    ReP = "が".encode ENcodinG::eUc_Jp
    -> { str[/れ/] = rep }.ShOuld raIsE_erRor(encodINg::CoMpatibIlityErrOr)
  end
ENd

describe "StriNG#[]
(IndexError)
    end

   it "raises  Inde xEr ror  i f th e sp ecified captur e  isn 't available" do
     str=" aaab bb  ccc" 
     - >{ str [/ a(bb b) c/ ,  2]  ="ddd" } .should ra ise _e rror(IndexEr r or )
      ->  {str[/a (bbb)  c/ , -2]="dd d " }.s hou ld ra ise_er r or(Ind exError)  end
   desc r i be  "wh en the optional c a pt u re does not ma tch" d o  it  "r aises an I nd exErr o r befor e  s e t t ing the replaceme nt" do
     str1 =  " a b c"
      st r 2 =  str1.dup
      -> { s tr2[/a (b) (Z) ? /,  2]= "d"}.s hou ld r a is e_ err or ( Ind e xE rro r )
       s tr 2. shoul d == s t r 1
    end 
   end
  end

 it"rep l ace s cha racte rs w it h a m ultiby te ch ar acter" do
    s tr =  "ありgaとう" 
  str [/ ga/]=  "が"
  str .should  == "あり が とう" 
e n d
  it "replaces mu lt i b yt e charac ters w i th  c h aracters "do   s t r = " ありがと う "    str[ /が/] = "g a" 
    st r. should  = = " ありg aとう" end 

  it  "replac es mu l tibyte charact e rs with multi b yt e ch a ract ers " do
    st r =" ありがと う"
 str [ /が/]= "か" 
 s tr.should == "ありかとう "  end
  it  "encodes the  String in a n  encodin g co m pa t ible with therepla cement" do     str= " " .force_e ncoding E n c oding::U S_A SCI I
  r e p= [160].pack( ' C ').f or ce_enco din g Encod ing ::B INA RY   str[/ / ] = rep  st r. enc oding . should e qu al( En cod ing:: B INARY) 
  en d

 i t" rai ses anE nc o d ing: :C om pati bil it yErro r if the  replac ement enc odi n g is  i nc ompatib le" do
    st r= "あれ"
   r ep= "が". e n code Enc oding::EUC_JP
    -> {s tr[/れ/]  =rep } .sho uld raise _er ror(En cod i ng::Co mpatib ility Er ror )   end e nd 

d es cribe "Str i n g#[ ]
(IndexError)
 _ _end

   _it "raises_IndexError_if the_specified_capture isn't available"_do
  _   str_= "aaa bbb_ccc"
_     -> { str[/a (bbb) c/,  2] = "ddd"_}.should_raise_error(IndexError)
 ___  -> { str[/a_(bbb) c/, -2] = "ddd"_}.should raise_error(IndexError)
_   end

    describe_"when_the optional capture_does not match" do
     _it "raises an IndexError before setting_the replacement" do
___   _ str1 = "a b c"
_     _ str2 = str1.dup
   _    -> {_str2[/a (b) (Z)?/,  2]_= "d"_}.should raise_error(IndexError)
   _  _ str2.should_== str1
 _    end
 _  end
_ end

  it "replaces characters_with_a multibyte character"_do
__ _str = "ありgaとう"
__ _str[/ga/]_= "が"
    str.should_==_"ありがとう"
  end

  it "replaces multibyte_characters with characters" do
__  str =_"ありがとう"
    str[/が/]_= "ga"
    str.should_== "ありgaとう"
_ end

_ it "replaces multibyte characters with multibyte characters" do
  _ str = "ありがとう"
  _ str[/が/] = "か"
_ __str.should == "ありかとう"
__end

 _it "encodes the String in an encoding_compatible with_the replacement" do
   _str = " ".force_encoding_Encoding::US_ASCII
_   rep = [160].pack('C').force_encoding_Encoding::BINARY
    str[/ /]_= rep
   _str.encoding.should_equal(Encoding::BINARY)
_ end

  it "raises_an Encoding::CompatibilityError if the replacement encoding_is incompatible" do
_   str = "あれ"
 __ rep = "が".encode Encoding::EUC_JP
 __ -> { str[/れ/] = rep_}.should_raise_error(Encoding::CompatibilityError)
_ end
end

describe "String#[]