From: Robert Klemme Date: 2013-04-29T00:42:45+09:00 Subject: Re: Store/Delete a string permanently in Ruby --20cf3074d9ac7a12c904db6d9e57 Content-Type: text/plain; charset=ISO-8859-1 On Fri, Apr 26, 2013 at 2:11 PM, cynic limbu wrote: > Can you create a program in ruby that can store a string or delete and > replace them permanently? > > Yes I know you can store and change a string that is stored in a > variable by writing > > var1 = "Hello world" > puts "The string of var1 is #{var1}, To change var1's string type in > anything" > var1 = gets.chomp > print "The string has been changed successfully, The string now is > #{var1}" > > > But whenever you restart the program again the var1's value changes back > to Hello world. How can you change or the var1's value or remove it > permanently? The most simple way is to store it in a file $ ls -l data ls: cannot access data: No such file or directory $ ./read.rb "Hello world" "Hello world ." $ ./read.rb "Hello world ." "Hello world . ." $ ./read.rb "Hello world . ." "Hello world . . ." $ ls -l data -rw-r--r--+ 1 Robert None 17 Apr 28 17:41 data $ cat data Hello world . . .$ cat -n read.rb 1 #!/usr/bin/ruby 2 3 file_name = ENV['HOME'] + "/data" 4 5 var1 = File.read(file_name) rescue "Hello world" 6 7 p var1 8 var1 << " ." 9 p var1 10 11 File.open(file_name, 'w') {|io| io.write(var1)} 12 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --20cf3074d9ac7a12c904db6d9e57 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Fri, Apr 26, 2013 at 2:11 PM, cynic limbu <= lists@ruby-forum.= com> wrote:
Can you create a program in ruby that can store a string o= r delete and
replace them permanently?

Yes I know you can store and change a string that is stored in a
variable by writing

var1 =3D "Hello world"
puts "The string of var1 is #{var1}, To change var1's string type = in
anything"
var1 =3D gets.chomp
print "The string has been changed successfully, The string now is
#{var1}"


But whenever you restart the program again the var1's value changes bac= k
to Hello world. How can you change or the var1's value or remove it
permanently?

The most simple way is to store it in a file=

$ ls -l data
ls: cannot access da= ta: No such file or directory
$ ./read.rb
"Hello world"
"Hello wo= rld ."
$ ./read.rb
"Hello world ."
=
"Hello world . ."
$ ./read.rb
"Hell= o world . ."
"Hello world . . ."
$ ls -l data
-rw-r--= r--+ 1 Robert None 17 Apr 28 17:41 data
$ cat data
Hell= o world . . .$ cat -n read.rb
=A0 =A0 =A01 =A0#!/usr/bin/ruby
=A0 =A0 =A02
=A0 =A0 =A03 =A0file_name =3D ENV['HOME'] + = "/data"
=A0 =A0 =A04
=A0 =A0 =A05 =A0var1 =3D= File.read(file_name) rescue "Hello world"
=A0 =A0 =A06=
=A0 =A0 =A07 =A0p var1
=A0 =A0 =A08 =A0var1 << " ."
=A0 =A0 =A09 =A0p va= r1
=A0 =A0 10
=A0 =A0 11 =A0File.open(file_name, 'w= ') {|io| io.write(var1)}
=A0 =A0 12
=A0
Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--20cf3074d9ac7a12c904db6d9e57--