"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 R.

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

I think gets isn’t compatible with ARGV / ARGF

using
filename = $*.dup

because
a = []
b = a
a.id == b.id

2013/4/19 Philip R. [email protected]

windwiny wrote in post #1106260:

using
filename = $*.dup

because
a = []
b = a
a.id == b.id

2013/4/19 Philip R. [email protected]

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:

windwiny,

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

using
filename = $*.dup

because
a = []
b = a
a.id == b.id

Thanks! - I should have thought of trying that.

Regards,

Phil.

puts filename
Philip R.

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]


Philip R.

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]