[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Use of array shift and blocks

Toby Rodwell

9/24/2006 4:22:00 PM

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



--
Posted via http://www.ruby-....

4 Answers

hubert depesz lubaczewski

9/24/2006 4:28:00 PM

0

On 2006-09-24, Toby Rodwell <ruby@tobyrodwell.com> 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

--
mój bo?e, spraw abym milcza3, dopóki sie nie upewnie, ?e naprawde mam
co? do powiedzenia. (c) 1998 depesz

dblack

9/24/2006 4:29:00 PM

0

Toby Rodwell

9/24/2006 4:51:00 PM

0

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


--
Posted via http://www.ruby-....

dblack

9/24/2006 5:03:00 PM

0