-2

I have code:

results = ["http://www.google.com", "http://www.yahoo.com.uk"]

results.each do |results| 
  File.open("#{results}.html", "w") do |file|
    file.write(RestClient.get(#{result}))
  end
end

When I run the program, it gives me an error:

syntax error, unexpected keyword_end, expecting ')'

I have two do-s and two end-s. Please help.

sawa
  • 165,429
  • 45
  • 277
  • 381

1 Answers1

6

# is used for string interpolation, but only within an appropriate string literal:

File.open("#{results}.html", "w")
#          ^^^^^^^^^^
#          string interpolation

Outside of strings, # starts a comment:

file.write(RestClient.get(#{result}))
#                         ^^^^^^^^^^^
#                         comment

Your editor should be able to highlight it accordingly:

vim screenshot

To "fix" it, use:

file.write(RestClient.get(result))

BTW, there's also another typo in your code:

results.each do |results|

Should be:

results.each do |result|

result then refers to a single element within your results array (double check your code for result vs. results).

Stefan
  • 109,145
  • 14
  • 143
  • 218
  • thanks! but using your 'fix' gives: 4:in `initialize': No such file or directory @ rb_sysopen - http://www.google.com.html (Errno::ENOENT) from trytrytry.rb:4:in `new' from trytrytry.rb:4:in `block in
    ' from trytrytry.rb:3:in `each' from trytrytry.rb:3:in `
    '
    – Henry Chan Jul 19 '15 at 10:07
  • Have also corrected "results" and 'result'. Thanks! – Henry Chan Jul 19 '15 at 10:07
  • new code: results = ["http://www.google.com", "http://www.yahoo.com.uk"] results.each do |result| file = File.new("#{result}.html", "w") do |file| file.write(RestClient.get(result)) file.close end end Still doesnt work – Henry Chan Jul 19 '15 at 10:10
  • You were correct the first time using `File#open` instead of `File#new`. Having`/` characters is causing problems I imagine, as in your original post. – james246 Jul 19 '15 at 10:17
  • @HenryChan you get a *"No such file or directory"* error, because `/` indicates a directory separator. `index.html` is a valid filename whereas `http://google.com` is not. – Stefan Jul 19 '15 at 10:19
  • thanks James. How do I correct / problem? I have actually tried replacing the URLs with "a", "b" and the code works. However, I want to save the file as its URL name, and it gives me error. – Henry Chan Jul 19 '15 at 10:22
  • 2
    @HenryChan If your question about 'unexpected keyword end' has been answered, but you need further help for _other_, unrelated problems in your code, please post new, specific questions via 'Ask Question' instead of here in the comments. If appropriate, link from one question to the other, or even both ways. – das-g Jul 19 '15 at 10:26
  • @Stefan I see! but all my URLs contain / . Do I just use escape character to solve the problem? Thanks! – Henry Chan Jul 19 '15 at 10:27
  • I would omit `http://` altogether and add it back in later if you need it, but yes you could replace the `/` with some encoded representation. This answer may be of use (not Ruby related): http://stackoverflow.com/questions/9847288 – james246 Jul 19 '15 at 10:29