On Fri, Mar 18, 2011 at 11:31 PM, Jeremy Bopp <jeremy / bopp.net> wrote:
> On 3/18/2011 10:22, Ruby Fan wrote:
>> Found way:
>>
>> b =3D []
>> a[3...7].size.times do b.push("B") end
>> a[3...7] =3D b
>>
> 3...7.each { |i| a[i] =3D "B" }
>
> class Array
> =A0def replace_range_with(range, replacement)
> =A0 =A0range.each { |i| self[i] =3D replacement }
> =A0 =A0self
> =A0end
> end
> a =3D ["A", "A", "A", "A", "A"]
> a.replace_range_with(2...4, "B") #=3D> ["A", "A", "B", "B", "A"]

in some cases, specifying the starting and  the length may be also handy, e=
g,

a=3D["A"]*10
#=3D> ["A", "A", "A", "A", "A", "A", "A", "A", "A", "A"]

a[3,4]=3D["B"]*4
#=3D> ["B", "B", "B", "B"]

a
#=3D> ["A", "A", "A", "B", "B", "B", "B", "A", "A", "A"]

and ff jeremy's wrapping,

class Array
def replacex start,length, item
  self[start,length]=3D[item]*length
end
end
#=3D> nil

a=3D["A"]*10
#=3D> ["A", "A", "A", "A", "A", "A", "A", "A", "A", "A"]

a.replacex  3, 4,"B"
#=3D> ["B", "B", "B", "B"]

a
#=3D> ["A", "A", "A", "B", "B", "B", "B", "A", "A", "A"]


best regards -botp