# Use of array shift and blocks

**URL:** <https://www.ruby-forum.com/t/use-of-array-shift-and-blocks/69827>\
**Category:** Ruby\
**Created:** [September 24, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/use-of-array-shift-and-blocks/69827 "2006-09-24T18:21:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/use-of-array-shift-and-blocks/69827/1 "2006-09-24T18:21:39Z")

</div>

(Something a bit more basic than the other thread about the array shfit  
bug!)

Given the following:

myArray=[“a”, “b”, “c”, “d”]  
until myArray.empty?  
myArray.shift { |letter|  
puts letter  
}  
end

… I would expect the letters a, b, c and d to be printed to the  
screen, but I don’t get anything (including no error message). Have I  
misunderstood something about array shift and/or blocks?

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/use-of-array-shift-and-blocks/69827/2 "2006-09-24T18:30:06Z")

</div>

Hi –

On Mon, 25 Sep 2006, Toby R. wrote:

> end
> 
> … I would expect the letters a, b, c and d to be printed to the  
> screen, but I don’t get anything (including no error message). Have I  
> misunderstood something about array shift and/or blocks?

shift doesn’t take a block. You can write one but shift won’t call  
it.

You could do this:

until array.empty?  
puts array.shift  
end

David

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/use-of-array-shift-and-blocks/69827/3 "2006-09-24T18:42:08Z")

</div>

On 2006-09-24, Toby R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Given the following:  
> myArray=[“a”, “b”, “c”, “d”]  
> until myArray.empty?  
> myArray.shift { |letter|  
> puts letter  
> }  
> end

change the code to:  
myArray=[“a”, “b”, “c”, “d”]  
until myArray.empty?  
letter = myArray.shift  
puts letter  
end

depesz

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/use-of-array-shift-and-blocks/69827/4 "2006-09-24T18:50:27Z")

</div>

unknown wrote:

> …  
> shift doesn’t take a block. You can write one but shift won’t call  
> it.
> 
> You could do this:
> 
> until array.empty?  
> puts array.shift  
> end
> 
> David

Many thanks David. I’m not sure I understand why shift doesn’t take a  
block, but thanks for confirming it.

regards  
Toby

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [September 24, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/use-of-array-shift-and-blocks/69827/5 "2006-09-24T19:04:10Z")

</div>

Hi –

On Mon, 25 Sep 2006, Toby R. wrote:

> > David
> 
> Many thanks David. I’m not sure I understand why shift doesn’t take a  
> block, but thanks for confirming it.

I expect it’s because shifting is a (conceptually) atomic operation.  
If you need to examine what got shifted, you can get the return value.  
If you need to examine the newly-shortened array, you can examine it  
directly. There’s really nothing for a block to do.

Consider, by way of contrast, something like String#sub, where you  
find something and replace it with something – and you might want to  
have a handle on the ‘something’, in the middle, so that you can  
manipulate it to generate the eventual replacement string.

David
