On Oct 4, 2006, at 8:19 AM, Peter Szinek wrote:

> Hello,
>
> Sorry for the lame question again, but even after a lot of googling  
> I still couldn't find the $subj., ie. something like:
>
> >>> "asdfghj"[2::2]
> 'dgj'
> (= every second letter, beginning from the 2nd index in the string)

I'm not going to call this as pretty, of course, but:

 >> test = "asdfghj"
=> "asdfghj"
 >> test[2..-1].gsub(/(.)./, "\\1")
=> "dgj"

Another way is to use step():

 >> result = ""
=> ""
 >> (2...test.size).step(2) { |i| result << test[i, 1] }
=> 2...7
 >> result
=> "dgj"

We can get that down to one line with enumerator:

 >> require "enumerator"
=> true
 >> (2...test.size).enum_for(:step, 2).inject("") { |r, i| r << test 
[i, 1] }
=> "dgj"

I do realize the Python you showed is sexier though.

> >>> "asdfghj"[::-1]
> 'jhgfdsa'
> (= the whole word, 1 steps backwards  - i.e. the reverse of the  
> string)

 >> test.reverse
=> "jhgfdsa"

;)

James Edward Gray II