Question: Is there any better way to slice a word in ruby?
My solution:

```
str="TAMILNADU"
pairs=[]
numPairs= str.length-1
i=0
while i<numPairs do 
  pairs[i] = str.slice(i,2)
  i+=1
end

```

Result

```
["TA", "AM", "MI", "IL", "LN", "NA", "AD", "DU"] 

```

Answer: 
Thanks(@Doorknob,@hirolau,@Stefan,@Cary Swoveland) for all of your responses find the benchmark report below.

Updated (Tue - Mar- 2015)

```
   require 'benchmark/ips'
    Benchmark.ips do |x|
      str = "TAMILNADU"
      x.report("1")  {
        pairs=[]
        numPairs= str.length()-1
        i=0
        while i<numPairs do
          pairs[i] = str.slice(i,2)
          i+=1
        end
      }
      x.report("2") {
        str.chars.each_cons(2).map(&:join)
      }
      x.report("3") {
        (0...str.size-1).map{|x| str[x,2]}
      }
      x.report("4"){
        str.scan(/(.)(?=(.))/).map(&:join)
      }
      x.report("5"){
        str.gsub(/(.)/,'\1\1')[1...-1].scan(/../)
      }
      x.report("6"){
        str.gsub(/./) { |c| c*2 }[1...-1].scan(/../)
      }
      x.compare!
    end
Calculating -------------------------------------
                   1    37.355k i/100ms
                   2     9.703k i/100ms
                   3    26.961k i/100ms
                   4     7.950k i/100ms
                   5     6.302k i/100ms
                   6     7.804k i/100ms
-------------------------------------------------
                   1    508.411k (±11.1%) i/s -      2.503M
                   2    107.568k (± 5.1%) i/s -    543.368k
                   3    332.923k (±10.2%) i/s -      1.672M
                   4     88.410k (± 4.5%) i/s -    445.200k
                   5     67.694k (± 4.8%) i/s -    340.308k
                   6     85.000k (± 5.8%) i/s -    429.220k

Comparison:
                   1:   508410.6 i/s
                   3:   332923.4 i/s - 1.53x slower
                   2:   107567.7 i/s - 4.73x slower
                   4:    88409.9 i/s - 5.75x slower
                   6:    85000.2 i/s - 5.98x slower
                   5:    67694.2 i/s - 7.51x slower

```
Another way using a regex:

```
str.gsub(/(.)/,'\1\1')[1...-1].scan(/../)
  #=> ["TA", "AM", "MI", "IL", "LN", "NA", "AD", "DU"] 

```

or a variant:

```
str.gsub(/./) { |c| c*2 }[1...-1].scan(/../)

```

Any another:

```
a = []
e = str.each_char
loop do
  a << (e.next << e.peek)
end
a

```

Edit: I added the last method after the benchmarking was done, so I ran the benchmark myself. It was dead last. (I like it anyway.)