Jamey Cribbs
11/2/2006 9:12:00 PM
Joe Ruby MUDCRAP-CE 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.
>
>
Nope. Now, you have two variables, base_name and line, referencing the
same object. Try:
base_name = line.dup
> BTW, is there a foreach function that automatically strips off the
> newlines from line? Returning the record separators is silly.
>
>
line.chomp!
> Currently hating Ruby,
>
Be a lover not a fighter! ;-)
Jamey
Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.