[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array prepending nil object

Serdar Kiliç

1/31/2006 5:54:00 AM

I'm going through Brian Schröder's tutorial and hit a small snag. The
code is (on page 16):

print 'Array as stack: '
stack = Array.new()
stack.push('a')
stack.push('b')
stack.push('c')
print stack.pop until stack.empty?

The display that I'm getting is:
Array as stack: cbanil

So somehow nil is creeping it's way into my array but I'm not sure
how. Any ideas?

--
Cheers,
Serdar Kilic
http://weblog....


4 Answers

Joel VanderWerf

1/31/2006 6:14:00 AM

0

Serdar Kiliç wrote:
> I'm going through Brian Schröder's tutorial and hit a small snag. The
> code is (on page 16):
>
> print 'Array as stack: '
> stack = Array.new()
> stack.push('a')
> stack.push('b')
> stack.push('c')
> print stack.pop until stack.empty?
>
> The display that I'm getting is:
> Array as stack: cbanil
>
> So somehow nil is creeping it's way into my array but I'm not sure
> how. Any ideas?

The nil is just the return value of the last expression, not part of the
output of the print calls.

You can see it more clearly in irb:

irb(main):001:0> print 'Array as stack: '
Array as stack: => nil
irb(main):002:0> stack = Array.new()
=> []
irb(main):003:0> stack.push('a')
=> ["a"]
irb(main):004:0> stack.push('b')
=> ["a", "b"]
irb(main):005:0> stack.push('c')
=> ["a", "b", "c"]
irb(main):006:0> print stack.pop until stack.empty?
cba=> nil

The printed output shows up on the left of the =>.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Brian Caswell

1/31/2006 6:17:00 AM

0

On Jan 31, 2006, at 12:54 AM, Serdar Kiliç wrote:
> print 'Array as stack: '
> stack = Array.new()
> stack.push('a')
> stack.push('b')
> stack.push('c')
> print stack.pop until stack.empty?
>
> The display that I'm getting is:
> Array as stack: cbanil
>
> So somehow nil is creeping it's way into my array but I'm not sure
> how. Any ideas?

You are running this inside irb. print returns nil, which you are
interpreting as being part of the output.

Try adding:

print "\n"

To the bottom of your example.

Brian

Serdar Kiliç

1/31/2006 6:24:00 AM

0

Thanks Joel. I did actually use irb but only through MINGW on WindowsXP, where unfortunately you don't get the descriptive "code/linemarker" thingys.On 31/01/06, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:> Serdar Kiliç wrote:> > I'm going through Brian Schröder's tutorial and hit a small snag. The> > code is (on page 16):> >> > print 'Array as stack: '> > stack = Array.new()> > stack.push('a')> > stack.push('b')> > stack.push('c')> > print stack.pop until stack.empty?> >> > The display that I'm getting is:> > Array as stack: cbanil> >> > So somehow nil is creeping it's way into my array but I'm not sure> > how. Any ideas?>> The nil is just the return value of the last expression, not part of the> output of the print calls.>> You can see it more clearly in irb:>> irb(main):001:0> print 'Array as stack: '> Array as stack: => nil> irb(main):002:0> stack = Array.new()> => []> irb(main):003:0> stack.push('a')> => ["a"]> irb(main):004:0> stack.push('b')> => ["a", "b"]> irb(main):005:0> stack.push('c')> => ["a", "b", "c"]> irb(main):006:0> print stack.pop until stack.empty?> cba=> nil>> The printed output shows up on the left of the =>.>> --> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407>>--Cheers,Serdar Kilichttp://weblog....

Serdar Kiliç

1/31/2006 6:27:00 AM

0

Thank you too Brian, that makes it quite clear.

On 31/01/06, Brian Caswell <bmc@shmoo.com> wrote:
>
> You are running this inside irb. print returns nil, which you are
> interpreting as being part of the output.
>
> Try adding:
>
> print "\n"
>
> To the bottom of your example.
>
> Brian

--
Cheers,
Serdar Kilic
http://weblog....