简体   繁体   中英

how to find the file path from the open command

I need to get the path of the file in fo variable so that i can pass the path to the unzip_file function. how do i get the path here?

url = 'http://www.dtniq.com/product/mktsymbols_v2.zip'
open(url, 'r') do |fo|
  puts "unzipfile "
  unzip_file(fo, "c:\\temp11\\")
end

In terms of how to do it I would do this:

  1. Find out the class of the object I am dealing with

     ruby-1.9.2-p290 :001 > tmp_file = open('tmp.txt', 'r') => #<File:tmp.txt> ruby-1.9.2-p290 :001 > tmp_file.class => File 
  2. Go look up the documentation for that class

    Google Search : ruby file

    Which returns Class: File ruby-doc.org => www.ruby-doc.org/core/classes/File.html

  3. Look at the methods. There is one called path -> looks interesting

If I haven't found an answer by now then

  1. Continue looking around google/stack overflow for a bit
  2. I really can't find a solution that matches my problem. Time to ask a question on here

Most of the time 1..3 should get you what you need. Once you learn to read the documentation you can do things a lot quicker. It's just trying to overcome how difficult it is to get into the docs when you first start.

The fo in your block should be a Tempfile so you can use the path method:

url = 'http://www.dtniq.com/product/mktsymbols_v2.zip'
open(url, 'r') do |fo|
    puts "unzipfile "
    unzip_file(fo.path, "c:\\temp11\\")
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM