[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why can't I write variables to a file?

Graham

6/4/2009 1:34:00 PM

Help please! I'm a Ruby newbie and can't get the variables in the following
script to write to a file (is it something to do with defining the
variables?).

# create variables
class Person
attr_accessor :name, :age
end
person_instance = Person.new
person_instance.name = "David"
person_instance.age= "37"

# write to an HTML file
File.open("test4.html", "w") do |f|
f.puts "<html><head></head><body><h3>person_instance.name
(person_instance.age), sat at the table</h3></body></html>"
end


2 Answers

Andrew Timberlake

6/4/2009 1:39:00 PM

0

On Thu, Jun 4, 2009 at 3:35 PM, Graham <graham.stow@stowassocs.co.uk> wrote=
:
> Help please! I'm a Ruby newbie and can't get the variables in the followi=
ng
> script to write to a file (is it something to do with defining the
> variables?).
>
> # create variables
> class Person
> =A0attr_accessor :name, :age
> end
> person_instance =3D Person.new
> person_instance.name =3D "David"
> person_instance.age=3D "37"
>
> # write to an HTML file
> File.open("test4.html", "w") do |f|
> =A0f.puts "<html><head></head><body><h3>person_instance.name
> (person_instance.age), sat at the table</h3></body></html>"
> end

Try
...
# write to an HTML file
File.open("test4.html", "w") do |f|
f.puts "<html><head></head><body><h3>#{person_instance.name}
(#{person_instance.age}), sat at the table</h3></body></html>"
end

Inside of "" you need to use #{<variable/method call>} to tell ruby to
execute code and not interpret the text as text.

Andrew Timberlake
http://ramblingso...

http://MyM... - The SIMPLE way to manage your savings

Graham

6/4/2009 3:17:00 PM

0

Thanks Andrew! Works fine. (I'm too use to Perl where you just provide the
variable).

"Andrew Timberlake" <andrew@andrewtimberlake.com> wrote in message
news:62ad3e390906040638o4c690cf4w128339bb992bc93@mail.gmail.com...
On Thu, Jun 4, 2009 at 3:35 PM, Graham <graham.stow@stowassocs.co.uk> wrote:
> Help please! I'm a Ruby newbie and can't get the variables in the
> following
> script to write to a file (is it something to do with defining the
> variables?).
>
> # create variables
> class Person
> attr_accessor :name, :age
> end
> person_instance = Person.new
> person_instance.name = "David"
> person_instance.age= "37"
>
> # write to an HTML file
> File.open("test4.html", "w") do |f|
> f.puts "<html><head></head><body><h3>person_instance.name
> (person_instance.age), sat at the table</h3></body></html>"
> end

Try
...
# write to an HTML file
File.open("test4.html", "w") do |f|
f.puts "<html><head></head><body><h3>#{person_instance.name}
(#{person_instance.age}), sat at the table</h3></body></html>"
end

Inside of "" you need to use #{<variable/method call>} to tell ruby to
execute code and not interpret the text as text.

Andrew Timberlake
http://ramblingso...

http://MyM... - The SIMPLE way to manage your savings