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

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.

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/

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

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:inopen’
app/views/pages/print_result.html.erb:1:in
`_app_views_pages_print_result_html_erb__106515637_82305240_211210089’

@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:inopen’
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

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

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz

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.

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
"

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

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

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz

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.
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.

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.


.
.
.
.
.
.

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. And that means the file name is crippled? I don’t think
so.

=> #<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) ------