How to reset a variable to the value in another variable?


(Kurt Euler) #1

All-

In line 8 in the code below, I want to reset the string variable “content2” with the value in string variable “intro”. However, what I’m finding is that line 8 sets up “content2” as some type of pointer to “intro”: The content2.gsub commands I’m making in lines 9 through 12 are affecting the value
of “intro” as if I were doing “intro.gsub” commands.

The solution has got to be simple, but it it illudes me. Your help appreciated! Thanks! -k euler

01 intro = ‘‘
02 File.open("./input/intro_subtemplate.txt") { |f| intro = f.read }
03
04 content = File.new("./input/main_readme_template.txt").read
05
06 IO.foreach("./input/packages.txt") { |x|
07 field = x.chop.split(’\t’)
08 content2 = intro
09 content2.gsub!(/<intro_subtemplate.txt>/, intro)
10 content2.gsub!(/<prod_all_cap>/, field[2])
11 content2.gsub!(/<prod_title_cap>/, field[1])
12 content2.gsub!(/<op_sys>/, field[3])
13 File.open("./output/#{field[0]}", “w+”) { |file| file.write(content2) }
14 }


(Ned Konz) #2

In line 8 in the code below, I want to reset the string variable
"content2" with the value in string variable “intro”. However, what
I’m finding is that line 8 sets up “content2” as some type of
pointer to “intro”: The content2.gsub commands I’m making in lines
9 through 12 are affecting the value of “intro” as if I were doing
"intro.gsub" commands.

Well, sure. You’re just pointing to intro. If you want to edit it,
just make a copy of it.

The solution has got to be simple, but it it illudes me. Your help
appreciated! Thanks! -k euler

08 content2 = intro

try
content2 = intro.dup

···

On Tuesday 11 June 2002 02:09 pm, Kurt Euler wrote:


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE


(Dossy) #3

All-

In line 8 in the code below, I want to reset the string variable “content2” with the value in string variable “intro”. However, what I’m finding is that line 8 sets up “content2” as some type of pointer to “intro”: The content2.gsub commands I’m making in lines 9 through 12 are affecting the value
of “intro” as if I were doing “intro.gsub” commands.

The solution has got to be simple, but it it illudes me. Your help appreciated! Thanks! -k euler

01 intro = ‘‘
02 File.open("./input/intro_subtemplate.txt") { |f| intro = f.read }
03
04 content = File.new("./input/main_readme_template.txt").read
05
06 IO.foreach("./input/packages.txt") { |x|
07 field = x.chop.split(’\t’)
08 content2 = intro

08 content2 = intro.dup

09 content2.gsub!(/<intro_subtemplate.txt>/, intro)
10 content2.gsub!(/<prod_all_cap>/, field[2])
11 content2.gsub!(/<prod_title_cap>/, field[1])
12 content2.gsub!(/<op_sys>/, field[3])
13 File.open("./output/#{field[0]}", “w+”) { |file| file.write(content2) }
14 }

– Dossy

···

On 2002.06.12, Kurt Euler keuler@portal.com wrote:


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)


(Armin Roehrl) #4

if you want more info as always Dave & Andy have done the work :slight_smile:
http://www.rubycentral.com/ref/ref_c_object.html#dup

similary
content2=String.new(intro)

···

Am Dienstag, 11. Juni 2002 23:24 schrieben Sie:

On Tuesday 11 June 2002 02:09 pm, Kurt Euler wrote:

In line 8 in the code below, I want to reset the string variable
"content2" with the value in string variable “intro”. However, what
I’m finding is that line 8 sets up “content2” as some type of
pointer to “intro”: The content2.gsub commands I’m making in lines
9 through 12 are affecting the value of “intro” as if I were doing
"intro.gsub" commands.

Well, sure. You’re just pointing to intro. If you want to edit it,
just make a copy of it.

The solution has got to be simple, but it it illudes me. Your help
appreciated! Thanks! -k euler

08 content2 = intro

try
content2 = intro.dup

Armin.


Armin Roehrl, http://www.approximity.com
Training, Development and Mentoring
OOP, XP, Java, Ruby, Smalltalk, Datamining, Parallel computing, Webservices
Koautor des ersten dt. Rubybuches http://approximity.com/rubybuch/
Naechstes Rubyseminar: 9/9-11/9 in Hamburg.

Don’t work hard, work smart.


(Jean-Hugues ROBERT) #5

Hello,

irb redefines exit() and exit!() so that they raise an exception
instead of exiting. This is sensible.

… well… I am experiencing a case where I need to exit. How
can I ? (btw: it is some background thread that will ask to exit)

Thanks,

Jean-Hugues


(James) #6

similary
content2=String.new(intro)

And if you don’t want a new String object, just the same object with new
contents, you could do

content2.replace( intro )

James


#7

In [ruby-talk:42318] the message: “irb, how to exit() ?”, on Jun/12 23:58(JST)
Jean-Hugues ROBERT writes:

Hello,

Hi,

irb redefines exit() and exit!() so that they raise an exception
instead of exiting. This is sensible.

irb don’t redefine exit!

… well… I am experiencing a case where I need to exit. How
can I ? (btw: it is some background thread that will ask to exit)

You can use exit!() or irb_exit_org() which is an alias of original exit().

– keiju


(Jean-Hugues ROBERT) #8

hello,

In [ruby-talk:42318] the message: “irb, how to exit() ?”, on Jun/12
23:58(JST)
Jean-Hugues ROBERT writes:

irb redefines exit() and exit!() so that they raise an exception
instead of exiting. This is sensible.

irb don’t redefine exit!

True ! I must have done something wrong when I tried it.

… well… I am experiencing a case where I need to exit. How
can I ? (btw: it is some background thread that will ask to exit)

You can use exit!() or irb_exit_org() which is an alias of original exit().

– keiju

Thanks a lot.

Yours,

Jean-Hugues

···

At 15:42 13/06/2002 +0900, keiju wrote:


Web: http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17