Hi, i'm new. plus one question

pop (and it's cousin 'shift') can't return a new array, because they're
busy returning the last (or first) element of the array.

pop and shift are modifying actions by definition - they translate to
"delete (and return) the last/first item - much the same way that
"delete" modifies the existing array.

What do you mean by 'rotate'? I know what that means in terms of a
multi-dimensional array, but since you ask 'string or array', I'm
guessing that's what you meant. And since you know about String#reverse,
I guess you don't mean rotate the string 180 degrees.

Maybe you want to rotate a portion of the array:

idea = ["a","b","c","d"]
idea[1..2] = idea[1..2].reverse
p idea

=> ["a","c","b","d"]

If you were trying to rotate a matrix:

require 'generator'

xy = [["a","b"],["c","d"],["e","f"]]
yx = SyncEnumerator.new(*xy).collect
p yx

=> [["a", "c", "e"], ["b", "d", "f"]]

I think there's another way in one of the standard libraries, but I
don't have a memory, so I just remember generally useful things like
SyncEnumerator and work from first principles.

ยทยทยท

-----Original Message-----
From: travis laduke [mailto:wrong@socal.rr.com]
Sent: Thursday, 22 September 2005 2:52 PM
To: ruby-talk ML
Subject: hi, i'm new. plus one question

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

#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################

Daniel Sheppard wrote:

pop (and it's cousin 'shift') can't return a new array, because they're
busy returning the last (or first) element of the array.

Well....

class Array
  def pop_
    ary = dup
    [ary.pop, ary]
  end
  def shift_
    ary = dup
    [ary.shift, ary]
  end
end

a = [1,2,3,4]
n, b = a.pop_ #=> [4, [1, 2, 3]]
n #=> 4
b #=> [1, 2, 3]
a #=> [1, 2, 3, 4]

Devin