Use of array shift and blocks

T

Toby Rodwell

(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?
 
H

hubert depesz lubaczewski

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
 
D

dblack

Hi --

(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?

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

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
T

Toby Rodwell

unknown said:
...
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
 
D

dblack

Hi --

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

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,213
Messages
2,571,105
Members
47,699
Latest member
lovelybaghel

Latest Threads

Top