String usage


#1

Hi all,
I have a question about string usage. I assign @test_string class
variable which is assigned the name of a text file.
@test_string = ‘file.txt’
When I was trying to use it as a parameter in a statement
info=File.open(@test_string) {|f| f.read.split},
it said “TypeError: can’t convert nil into String”. I check the type of
@test_string and it said String object. What am I missing?

Then I try to use to_s method, and it said "Errno::ENOENT: No such file
or directory - "

Could someone explain what is going here? Pls kind on me as I am new to
Ruby.

Thanks,
Maung


#2

never mind, I figured that out because I was using it as class variable
and I didn’t instantiate the object.
Thanks.
Maung

Maung A. wrote:

Hi all,
I have a question about string usage. I assign @test_string class
variable which is assigned the name of a text file.
@test_string = ‘file.txt’
When I was trying to use it as a parameter in a statement
info=File.open(@test_string) {|f| f.read.split},
it said “TypeError: can’t convert nil into String”. I check the type of
@test_string and it said String object. What am I missing?

Then I try to use to_s method, and it said "Errno::ENOENT: No such file
or directory - "

Could someone explain what is going here? Pls kind on me as I am new to
Ruby.

Thanks,
Maung