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
theloo
August 26, 2011, 10:49pm
2
On 26 August 2011 21:22, thelo.g thelo [email protected] wrote:
string = “archives/sequences/” + @filename => filename being
“archives-2011.xml”
File.new(string)
it doesn’t work!
ruby tells me it cannot find the file
Output the value of “string” and see what it is.
theloo
August 26, 2011, 10:50pm
3
On 26 August 2011 21:22, thelo.g thelo [email protected] wrote:
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
You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Colin
https://plus.google.com/111605230843729345543/
theloo
August 26, 2011, 11:11pm
4
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
theloo
August 26, 2011, 11:14pm
5
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
theloo
August 26, 2011, 11:09pm
6
No such file or directory -
./archives/results/Archives-2011-08-26-09-56-00-UTC.xml
Extracted source (around line #1 ):
1: 
Résultat
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’
theloo
August 26, 2011, 11:23pm
7
@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: 
Résultat
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
theloo
August 26, 2011, 11:45pm
8
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
On Aug 26, 2011, at 23:23 , thelo.g thelo wrote:
<% string = “./archives/results/” + r %>
–
Posted via http://www.ruby-forum.com/ .
–
You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
theloo
August 26, 2011, 11:51pm
9
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: 
Résultat
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”
Your output proves nothing.
theloo
August 26, 2011, 11:51pm
10
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
"
theloo
August 26, 2011, 11:57pm
11
I meant when i replace string by
“./archives/results/Archives-2011-08-26-09-56-00-UTC.xml”, which is
normaly similar to ‘./archives/results’ + r
theloo
August 27, 2011, 12:02am
12
On Aug 26, 2011, at 23:51 , thelo.g thelo wrote:
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 …
–
Posted via http://www.ruby-forum.com/ .
–
You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected] .
To unsubscribe from this group, send email to
[email protected] .
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en .
–
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
theloo
August 27, 2011, 12:06am
13
What do you mean? and if this is the case, how can I fix it?
theloo
August 27, 2011, 2:54am
14
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:
puts “–>#{some_var}<—”
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.
A newline is a character just like any other character. 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 filename in your current directory on
your
computer, try adding a
\n to the end of the filename stored in some_file. Does the code still
work?
To get rid of a newline at the end of a string, use chomp(). chomp()
removes a newline at the end of a string if present, otherwise it does
nothing.
theloo
August 27, 2011, 3:03am
15
Tom M. 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.
…
.
.
.
.
.
.
theloo
August 27, 2011, 9:52am
16
Thank you guys a lot, 7stud was right, I added chomp and it works!
Hence, \n makes a difference.
theloo
August 27, 2011, 1:28pm
17
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. And that means the file name is crippled? I don’t think
so.
theloo
August 27, 2011, 1:20pm
18
=> #<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)
–
----)(-----
Luis Mondesi
Maestro Debiano
----- START ENCRYPTED BLOCK (Triple-ROT13) ------
Gur Hohagh [Yvahk] qvfgevohgvba oevatf gur fcvevg bs Hohagh gb gur
fbsgjner
jbeyq.
----- END ENCRYPTED BLOCK (Triple-ROT13) ------