0

In the default skeleton code for a Ruby gem, the .gemspec file inside of the Gem::Specification class there is this line:

spec.files = `git ls-files -z`.split("\x0") <- please note the PS at the end of this question

When I run git ls-files -z in the terminal I get

.gitignoreGemfileLICENSE.txtREADME.mdRakefiledogeify.gemspeclib/dogeify.rblib/dogeify/version.rb

What exactly is the .split("\x0") method producing as the output?

PS: In reference to the first line of code, does the `` characters allow the terminal to execute code and combine the result with a Ruby method?

1 Answer 1

11

The git(1) help says:

$ git help ls-files
[...]
    -z
        \0 line termination on output.

So when you say git ls-files -z, it will list the files with zero bytes as delimiters between the file names. You'll see similarly formatted output when you say find ... -print0 or use xargs -0. The reason that you'd use a zero byte as a delimiter is that they can't appear in a file name but any other delimiter (such as a space) can.

Then back in Ruby, you need to unpack that \x0-delimited list. If you string.split("\x0"), you'll break string apart on zero bytes ("\x0") and return the parts as an array; for example:

> "where\x0is\x0pancakes\x0house?".split("\x0")
 => ["where", "is", "pancakes", "house?"] 

So git ls-files -z is give you a list of files with zero bytes between the file names and split("\x0") breaks that string into an array of file names.

2
  • Thank you for this answer. If there is code in a Ruby file surrounded by backticks: ``, does that mean you can attach a Ruby method on the end of it?
    – Aaron
    Aug 7, 2014 at 22:52
  • The backticks are really a method in Kernel that returns a String: ruby-doc.org/core-2.1.2/Kernel.html#method-i-60 Aug 7, 2014 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.