When is File.join useful?
There is another, subtle difference:
File.join('foo','bar')
#=> "foo/bar"
['foo','bar'].join('/')
#=> "foo/bar"
But, if you pass an argument already ending with /
(which is quite often when working with paths), you won't have two slashes in the result:
File.join('foo/','bar')
#=> "foo/bar"
['foo/','bar'].join('/')
#=> "foo//bar"
Why should I use File.join()?
Any of the following :
File.join("first","second")
File.join("first/","second")
File.join("first","/second")
File.join("first/","/second")
Will return
=> "first/second"
Could it be a good reason for you ?
That's only one example I can think of.
Actually, your goal is not to concatenate 2 strings, your goal is creating a path. This looks like a strong reason to use File.join
to me.
Confusion about Dir[] and File.join() in Ruby
File.join() simply concats all its arguments with separate slash.
For instance,
File.join("a", "b", "c")
returns "a/b/c". It is alsmost equivalent to more frequently used Array's join method, just like this:
["hello", "ruby", "world"].join(", ")
# => "hello, ruby, world"
Using File.join(), however, additionaly does two things: it clarifies that you are getting something related to file paths, and adds '/' as argument (instead of ", " in my Array example). Since Ruby is all about aliases that better describe your intentions, this method better suits the task.
Dir[] method accepts string or array of such strings as a simple search pattern, with "*" as all files or directories, and "**" as directories within other directories. For instance,
Dir["/var/*"]
# => ["/var/lock", "/var/backups", "/var/lib", "/var/tmp", "/var/opt", "/var/local", "/var/run", "/var/spool", "/var/log", "/var/cache", "/var/mail"]
and
Dir["/var/**/*"]
# => ["/var/lock", "/var/backups", "/var/backups/dpkg.status.3.gz", "/var/backups/passwd.bak" ... (all files in all dirs in '/var')]
It is a common and very convinient way to list or traverse directories recursively
What is the purpose of the load File.Join line in config.rb for Sencha Touch?
According to the Ruby
docs:
Returns a new string formed by joining the strings using File::SEPARATOR.
Here is a link to the docs:
http://www.ruby-doc.org/core-1.9.3/File.html
Basically, File.join
joins the strings together, returning a new string used in a file path.
skip first parameter in File.join
Don't forget that Ruby can splat arguments, so this is actually pretty easy. You can either selectively add it:
args = %w[ ** *.jpg ]
if (options.path)
args.unshift(options.path)
end
File.join(*args)
Or you can just put it in by default and strip it out if it's nil
:
File.join(*[ options.path, '**', '*.jpg' ].compact)
It's important to note that the only things in Ruby that are logically false are nil
and false
so a test for nil?
is only required if it's possible that value might be literal false
. If that's not the case a regular if
will suffice.
Joining several files based on first file
You can use join
but you need to set a few options:
join -a1 -o1.1,2.2,2.3 -e "." <(sort test_1) <(sort test_2) > tmp_1
join -a1 -o1.1,1.2,1.3,2.2,2.3,2.4,2.5 -e "." <(sort tmp_1) <(sort test_3) > output
Explanation: Your example is in 3 files ('test_1' 'test_2' and 'test_3') so the first step is to combine test_1 and test_2 into a temporary file (tmp_1) using join. The -a1
option is telling join to look at the first column in both files for 'matches', the -o1.1,2.2,2.3
is telling join to print the first column of the first file (1.1), the second column of the second file (2.2) and the third column of the second file (2.3). The -e "."
is telling join to fill in any blanks with a dot. The inputs need to be sorted, so <(sort file)
is used to sort the contents before being joined. Next step is to join the temp file with the test_3 file. The options are the same, but different columns are printed.
MySQL statement combining a join and a count?
SELECT fol.*
, ( SELECT COUNT(*)
FROM files fil
WHERE fil.Folder = fol.Folder
) AS "Files"
FROM folders fol
WHERE fol.userId = 16
It's called a correlated subquery.
http://dev.mysql.com/doc/refman/5.1/en/correlated-subqueries.html
Using ADO to join and query text files
Yes. It is possible and it works. I was intrigued by your question so I tried it out myself. The text driver doesn't understand the bracketing on the fieldnames, only on the table name.
So use aliases for the field names like this:
Select tb1.[fieldname], tb2.[fieldname] From [file_name.txt] as tb1
Inner Join [file_name2.txt] as tb2
On tb1.[fieldname]=tb2.[fieldname]
What worked for me:
SELECT tb1.[Month], tb2.[Year] FROM [Text;DATABASE=E:\].[MoneyAndCreditStats 0409 to 0417.csv] as tb1
INNER JOIN [Text;DATABASE=E:\].[StackaOverFlowTest.csv] as tb2 ON
tb2.[Month] = tb1.[Month] AND
tb1.[Year] = tb2.[Year]
Text driver is a nifty tool especially when shuffling data formats/files around for Business Intelligence.
How to use a variable for a file path? Ruby
You can use variable inside string, following way:
array.to_csv("#{location}\FileName.csv")
Related Topics
Ruby Sandboxing VS. Integrating a Scripting Language
Mechanize How to Get Current Url
Why Does Date Exist in Ruby Before It Is Required
How to Get the File Extension from a Url
Passenger: Cannot Load Such File Rubygems/Builder
Automatically Precompile Assets Before Pushing to Heroku
How to Store Enum as String to Database in Rails
Rails Change Routing of Submit in Form_For
Get Time Object at Start of Day in a Particular Time Zone
Connecting to Web Services Using Rails (Http Requests)
How to Configure Config.Yml So That I Can Install Devkit
Does Rails Come with a "Not Authorized" Exception
Facebook Open Graph from Rails Heroku
Is a Global Variable Defined Inside a Sinatra Route Shared Between Requests