"gets" overwrites string - why does this happen?

People,

If I run this script with a text filename parameter:

#!/usr/bin/ruby

puts $*
filename = $*
puts filename
gets
puts $_
puts filename

- the last puts is empty! Why does this happen?

Thanks,

Phil.

···

--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

I think gets isn't compatible with ARGV / ARGF

···

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

using
   filename = $*.dup

because
   a =
   b = a
   a.__id__ == b.__id__

···

2013/4/19 Philip Rhoades <phil@pricom.com.au>

People,

If I run this script with a text filename parameter:

#!/usr/bin/ruby

puts $*
filename = $*
puts filename
gets
puts $_
puts filename

- the last puts is empty! Why does this happen?

Thanks,

Phil.
--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

windwiny wrote in post #1106260:

using
   filename = $*.dup

because
   a =
   b = a
   a.__id__ == b.__id__

Ah yes, in this case gets is modifying $* and therefore filename as
well.
Please disregard my previous answer, I didn't know about that behaviour
of gets :slight_smile:

···

2013/4/19 Philip Rhoades <phil@pricom.com.au>

--
Posted via http://www.ruby-forum.com/\.

windwiny,

using
filename = $*.dup

because
a =
b = a
a.__id__ == b.__id__

Thanks! - I should have thought of trying that.

Regards,

Phil.

···

On 2013-04-19 18:57, windwiny wrote:

2013/4/19 Philip Rhoades <phil@pricom.com.au>

People,

If I run this script with a text filename parameter:

#!/usr/bin/ruby

puts $*
filename = $*
puts filename
gets
puts $_
puts filename

- the last puts is empty! Why does this happen?

Thanks,

Phil.
--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au