Begin ... while ...?

— 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^^

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

2008/8/21 Kyung won Cheon [email protected]:

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

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

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
Gnu/Linux, FLOSS, Espiritismo, e eu por mim mesmo 8^I

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.