On 22/09/05, travis laduke <wrong / socal.rr.com> wrote: > I've been forced to work on some php lately and found myself > thinking: "man, this sucks. i wish it was more like supercollider" > then i remembered supercollider is ruby-influenced so i started > reading about ruby. > > Here's my question: is there a common way to rotate a string or an > array? > > I thought i could do some combination of .pop and .unshift or > something, but i ran into this problem: > > idea = ["a","b","c","d"] > x = idea.pop > puts idea > > why is idea changed? how do i make it stay the same? > why is it different than: > > x = idea.reverse > > where idea is left alone and only x is the reversed array? > > > > -travis > > Hope this helps: bschroed@black:~/svn/projekte/ruby-things$ cat rotate.rb module Rotate def rotate(steps = 1) steps = steps % self.length self[-steps..-1].concat self[0...-steps] end end class String include Rotate end class Array include Rotate end string = "I am a String" array = string.split 10.times do | i | p string.rotate(i) p array.rotate(i) end bschroed@black:~/svn/projekte/ruby-things$ ruby rotate.rb "I am a String" ["I", "am", "a", "String"] "gI am a Strin" ["String", "I", "am", "a"] "ngI am a Stri" ["a", "String", "I", "am"] "ingI am a Str" ["am", "a", "String", "I"] "ringI am a St" ["I", "am", "a", "String"] "tringI am a S" ["String", "I", "am", "a"] "StringI am a " ["a", "String", "I", "am"] " StringI am a" ["am", "a", "String", "I"] "a StringI am " ["I", "am", "a", "String"] " a StringI am" ["String", "I", "am", "a"] regards, Brian -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/