On 02/11/06, Joe Ruby MUDCRAP-CE <joeat303 / yahoo.com> wrote:
> I have:
>
> File.foreach(data_file) do |line|
>   line.strip!
>
>   puts line
>
>   base_name = line
>
>   puts line
>
>   base_name.sub!('www.', '')
>   base_name.sub!(/\.\w+$/, '')
>
>   puts line
> end
>
> Which outputs:
>
> www.domain.com
> www.domain.com
> domain
>
> WHY is var line getting changed by operations on var base_name? Isn't
> 'base_name = line' supposed to create a copy? '=' in this case seems to
> be acting like an alias or something.
>
> BTW, is there a foreach function that automatically strips off the
> newlines from line? Returning the record separators is silly.
>
> Currently hating Ruby,
> Joe
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

It doesn't create a copy, both variables reference the same object. If
you don't want to modify the original value use sub rather than gsub!

Farrel