strings issue

Hi everybody,

I ve got a problem with strings and the class File in rails Indeed, when i try to do this: File.new("archives/sequences/archives-2011.xml") it works perfectly, however when i do string = "archives/sequences/" + @filename => filename being "archives-2011.xml" File.new(string)

it doesn't work! ruby tells me it cannot find the file

does anyone could tell me why?

Thanks

Output the value of "string" and see what it is.

Hi everybody,

I ve got a problem with strings and the class File in rails Indeed, when i try to do this: File.new("archives/sequences/archives-2011.xml") it works perfectly, however when i do string = "archives/sequences/" + @filename => filename being "archives-2011.xml" File.new(string)

it doesn't work! ruby tells me it cannot find the file

Show us the exact error message. Copy and paste it into the post, don't retype it.

Colin

No such file or directory - ./archives/results/Archives-2011-08-26-09-56-00-UTC.xml Extracted source (around line #1):

1: <h1>Résultat</h1> <p>id :<%= @result %>

<% r = Result.find(@result).nameFile %> <% string = "./archives/results/" + r %> <% f = File.open(string) %>

Rails.root: /home/thelo/RoR/ihm_test

Application Trace | Framework Trace | Full Trace app/views/pages/print_result.html.erb:1:in `initialize' app/views/pages/print_result.html.erb:1:in `open' app/views/pages/print_result.html.erb:1:in `_app_views_pages_print_result_html_erb__106515637_82305240_211210089'

thelo.g thelo wrote in post #1018716:

Hi everybody,

I ve got a problem with strings and the class File in rails Indeed, when i try to do this: File.new("archives/sequences/archives-2011.xml") it works perfectly, however when i do string = "archives/sequences/" + @filename => filename being "archives-2011.xml" File.new(string)

it doesn't work! ruby tells me it cannot find the file

You lie:

str1 = 'archives/sequences/archives-2011.xml'

@filename = 'archives-2011.xml' str2 = "archives/sequences/" + @filename

puts str1 == str1

--output:-- true

And the output of the string is:

ActionView::Template::Error (No such file or directory - ./archives/results/Archives-2011-08-26-09-56-00-UTC.xml

@7stud no I do not, I might have bad explained the problem. the thing is when i perform the following code:

No such file or directory - ./archives/results/Archives-2011-08-26-09-56-00-UTC.xml Extracted source (around line #1):

<% r = Result.find(@result).nameFile %> <% string = "./archives/results/" + r %> <% f = File.open(string) %>

I got this error: No such file or directory - ./archives/results/Archives-2011-08-26-09-56-00-UTC.xml Extracted source (around line #1):

1: <h1>Résultat</h1> <p>id :<%= @result %>

<% r = Result.find(@result).nameFile %> <% string = "./archives/results/" + r %> <% f = File.open(string) %>

Rails.root: /home/thelo/RoR/ihm_test

Application Trace | Framework Trace | Full Trace app/views/pages/print_result.html.erb:1:in `initialize' app/views/pages/print_result.html.erb:1:in `open' app/views/pages/print_result.html.erb:1:in `_app_views_pages_print_result_html_erb__106515637_82305240_211210089'

and in the console ActionView::Template::Error (No such file or directory - ./archives/results/Archives-2011-08-26-09-56-00-UTC.xml

So when i replace string by "Archives-2011-08-26-09-56-00-UTC.xml" everything is ok, no problem. I just want to understand why and fix this problem

Thanks

full patch of "archives" is? if under rails root, then change code to

string = Rails.root.to_s+'/archives/results/'+r

or write down full path to archives directory

tom

I tried but it doesn't work, is it possible that the mistake has a ling with \n at the end of r? because when I do: puts '"' + r + '"'

I get "Archives-2011-08-26-09-56-00-UTC.xml "

thelo.g thelo wrote in post #1018731:

@7stud no I do not, I might have bad explained the problem. the thing is when i perform the following code:

No such file or directory - ./archives/results/Archives-2011-08-26-09-56-00-UTC.xml Extracted source (around line #1):

<% r = Result.find(@result).nameFile %> <% string = "./archives/results/" + r %> <% f = File.open(string) %>

I got this error: No such file or directory - ./archives/results/Archives-2011-08-26-09-56-00-UTC.xml Extracted source (around line #1):

1: <h1>Résultat</h1> <p>id :<%= @result %>

<% r = Result.find(@result).nameFile %> <% string = "./archives/results/" + r %>

So when i replace string by "Archives-2011-08-26-09-56-00-UTC.xml"

('./archives/results' + r) is not the same thing as "Archives-2011-08-26-09-56-00-UTC.xml"

You have proved nothing.

I meant when i replace string by "./archives/results/Archives-2011-08-26-09-56-00-UTC.xml", which is normally similar to './archives/results' + r

I tried but it doesn't work, is it possible that the mistake has a ling with \n at the end of r? because when I do: puts '"' + r + '"'

I get "Archives-2011-08-26-09-56-00-UTC.xml "

\n doesn't matter

ruby-1.8.7-p174 :006 > File.new("./testfile",'w') => #<File:./testfile> ruby-1.8.7-p174 :007 > File.new("./testfile\n",'w') => #<File:./testfile

you just cripple the filename ..

What do you mean? and if this is the case, how can I fix it?

thelo.g thelo wrote in post #1018739:

I tried but it doesn't work, is it possible that the mistake has a ling with \n at the end of r? because when I do: puts '"' + r + '"'

I get "Archives-2011-08-26-09-56-00-UTC.xml "

It's not only possible, that is exactly what is at the end of r. When you are having such a problem, it is easier to see what the string actually contains by doing one of the following:

1) puts "-->#{some_var}<---"

2) p r

which is the same as:

puts r.inspect

Here is an example:

str = "hello world"

p str

--output:-- "hello\nworld"

And of course the newline makes a difference in the file name. You can test that yourself:

some_file = "html.htm"

File.open(some_file) do |f|   puts f.read end

Once you get that to work with a file on your file system, try adding a \n to the end.

Tom Meinlschmidt wrote in post #1018745:

\n doesn't matter

ruby-1.8.7-p174 :006 > File.new("./testfile",'w') => #<File:./testfile> ruby-1.8.7-p174 :007 > File.new("./testfile\n",'w') => #<File:./testfile

you just cripple the filename ..

Really?? I don't see a crippled filename.

.. . . . . . .

=> #<File:./testfile

you just cripple the filename …

Really?? I don’t see a crippled filename.

You don’t see that > is in the next line? the \n char is being interpreted.

Just do: string.chomp

This should work (as suggested before):

string = Rails.root.to_s+'/archives/results/'+r.chomp

If you really want to be portable you should be writing file paths like: File.join(Rails.root,“archives”,“results”,r.chomp)

Thank you guys a lot, 7stud was right, I added chomp and it works! Hence, \n makes a difference.

Luis Mondesi wrote in post #1018782:

=> #<File:./testfile

>> > > you just cripple the filename .. >

Really?? I don't see a crippled filename.

You don't see that > is in the next line?

Yes, I see exactly that.

the \n char is being interpreted.

No kidding.