# Begin ... while ...?

**URL:** <https://www.ruby-forum.com/t/begin-while/146450>\
**Category:** Ruby\
**Created:** [August 21, 2008, 9:45am UTC](https://www.ruby-forum.com/t/begin-while/146450 "2008-08-21T09:45:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [August 21, 2008, 9:45am UTC](https://www.ruby-forum.com/t/begin-while/146450/1 "2008-08-21T09:45:11Z")

</div>

— test1.rb —

def test\_begin\_while(\*l)  
b = begin a = l.shift end while !l.empty?  
end

puts test\_begin\_while(1,2,3)

# =\> nil

— test2.rb —

def test\_begin\_while(\*l)  
b = begin a = l.shift end while !l.empty?  
b  
end

puts test\_begin\_while(1,2,3)

# =\> 3

###############

# Help Me^^

###############

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [August 21, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/begin-while/146450/2 "2008-08-21T14:12:06Z")

</div>

2008/8/21 Kyung won Cheon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ###############
> 
> # Help Me^^
> 
> ###############

irb(main):005:0\> def test\_begin\_while(\*l)  
irb(main):006:1\> b = begin a = l.shift; p [a,b]; a end while !l.empty?  
irb(main):007:1\> end  
=\> nil  
irb(main):008:0\> test\_begin\_while 1,2,3  
[1, nil]  
[2, 1]  
[3, 2]  
=\> nil  
irb(main):009:0\> def test\_begin\_while(\*l)  
irb(main):010:1\> b = ( begin a = l.shift; p [a,b]; a end while  
!l.empty? )  
irb(main):011:1\> end  
=\> nil  
irb(main):012:0\> test\_begin\_while 1,2,3  
[1, nil]  
[2, nil]  
[3, nil]  
=\> nil  
irb(main):013:0\> def test\_begin\_while(\*l)  
irb(main):014:1\> ( b = begin a = l.shift; p [a,b]; a end ) while  
!l.empty?  
irb(main):015:1\> end  
=\> nil  
irb(main):016:0\> test\_begin\_while 1,2,3  
[1, nil]  
[2, 1]  
[3, 2]  
=\> nil

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [August 24, 2008, 10:33am UTC](https://www.ruby-forum.com/t/begin-while/146450/3 "2008-08-24T10:33:01Z")

</div>

Em Qui, 2008-08-21 Ã s 16:41 +0900, Kyung won Cheon escreveu:

> ###############
> 
> # Help Me^^
> 
> ###############

If I did well understand, you just want to catch the values passed in as  
args to test\_begin\_while in an array. I’d do:

irb(main):020:0\> def test\_begin\_while(\*l)  
irb(main):021:1\> a = []  
irb(main):022:1\> b = begin a \<\< l.shift end while !l.empty?  
irb(main):023:1\> a  
irb(main):024:1\> end  
=\> nil  
irb(main):025:0\> test\_begin\_while 1, 2, 3  
=\> [1, 2, 3]

Hope this helps.

Regards,

## – angico

home page: [www.angico.org](http://www.angico.org)  
Gnu/Linux, FLOSS, Espiritismo, e eu por mim mesmo 8^I

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [August 24, 2008, 11:20am UTC](https://www.ruby-forum.com/t/begin-while/146450/4 "2008-08-24T11:20:45Z")

</div>

Kyung won Cheon wrote:

> — test1.rb —
> 
> def test\_begin\_while(\*l)  
> b = begin a = l.shift end while !l.empty?  
> end
> 
> puts test\_begin\_while(1,2,3)
> 
> ###############
> 
> # Help Me^^
> 
> ###############

It works like this because your line works like

(b = begin a = l.shift end) while !l.empty?

and the ‘command while condition’ construct returns nil.
