Array range values assignment

Discussion in 'Ruby' started by Ruby Fan, Mar 18, 2011.

  1. Ruby Fan

    Ruby Fan Guest

    a = ["A","A","A","A","A", "A","A","A","A","A"]
    a[3...7] = "B"

    QUESTION:
    after code executes a = ["A", "A", "A", "B", "A", "A", "A"]
    what should I do to make a = ["A", "A", "A", "B", "B", "B", "B", "A",
    "A", "A"]

    --
    Posted via http://www.ruby-forum.com/.
    Ruby Fan, Mar 18, 2011
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    Just
    a[3...7] = ['B','B','B','B'].

    Works at least in 1.9.2.

    2011/3/18 Ruby Fan <>

    > a = ["A","A","A","A","A", "A","A","A","A","A"]
    > a[3...7] = "B"
    >
    > QUESTION:
    > after code executes a = ["A", "A", "A", "B", "A", "A", "A"]
    > what should I do to make a = ["A", "A", "A", "B", "B", "B", "B", "A",
    > "A", "A"]
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Gunther Diemant, Mar 18, 2011
    #2
    1. Advertising

  3. Ruby Fan

    Ruby Fan Guest

    I understand that :)

    But I need some universal way to change values of arrays with 10+
    elements

    --
    Posted via http://www.ruby-forum.com/.
    Ruby Fan, Mar 18, 2011
    #3
  4. Ruby Fan

    Ruby Fan Guest

    Found way:

    b = []
    a[3...7].size.times do b.push("B") end
    a[3...7] = b

    is there anything simplier?

    --
    Posted via http://www.ruby-forum.com/.
    Ruby Fan, Mar 18, 2011
    #4
  5. Ruby Fan

    Jeremy Bopp Guest

    On 3/18/2011 10:22, Ruby Fan wrote:
    > Found way:
    >
    > b = []
    > a[3...7].size.times do b.push("B") end
    > a[3...7] = b
    >
    > is there anything simplier?


    Not necessarily any simpler, but perhaps a bit more efficient:

    3...7.each { |i| a = "B" }

    Wrap it up into a nice method:

    class Array
    def replace_range_with(range, replacement)
    range.each { |i| self = replacement }
    self
    end
    end

    a = ["A", "A", "A", "A", "A"]
    a.replace_range_with(2...4, "B") #=> ["A", "A", "B", "B", "A"]


    -Jeremy
    Jeremy Bopp, Mar 18, 2011
    #5
  6. Ruby Fan

    Ruby Fan Guest

    Ruby Fan, Mar 18, 2011
    #6
  7. Ruby Fan

    botp Guest

    On Fri, Mar 18, 2011 at 11:31 PM, Jeremy Bopp <> 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 =3D "B" }
    >
    > class Array
    > =A0def replace_range_with(range, replacement)
    > =A0 =A0range.each { |i| self =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
    botp, Mar 18, 2011
    #7
  8. Nagachaitanya Vellanki, Mar 18, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    46
    Views:
    966
    Antoon Pardon
    Jul 25, 2006
  2. Lambda
    Replies:
    2
    Views:
    393
    James Kanze
    Jul 16, 2008
  3. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    151
    Tomoyuki Kosimizu
    Nov 27, 2003
  4. David Bird
    Replies:
    1
    Views:
    209
    Tiago Macedo
    Jun 23, 2008
  5. Joey Zhou
    Replies:
    5
    Views:
    234
    Joey Zhou
    Apr 15, 2011
Loading...

Share This Page