From: Robert Klemme Date: 2012-09-24T21:42:35+09:00 Subject: Re: Insert at index On Mon, Sep 24, 2012 at 2:28 PM, Geena Tho wrote: > Thank you Wayne, But > > (0..n-1).each do |i| > st.insert(i,ch) > end > This is my code, st is my string. I need to insert a character, say ch. > n is 3 > > for example, if st = 'ab' and ch = 'c' i get: > abc > aabc > aaabc > But the desired output is > abc > bac > bca The last two outputs have order of "a" and "b" reversed - that can never be the result of simply inserting something into "ab". Are those typos or is it intentional? If it is intentional, what is it that you _really_ want? In case of type, please have a look: irb(main):001:0> st = 'ab' => "ab" irb(main):002:0> ch = 'c' => "c" irb(main):003:0> 3.times {|i| puts st.dup.insert(i, ch)} cab acb abc => 3 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/