hack-is-art hack-is-art - 1 month ago 7
Git Question

How to output file name inside system(git log) command in ruby script?

The problem I encounter is that I don't manage to put the name of the file inside the below screen output inside git log using pretty format flag.

An extract of my code is the following:

filename = File.basename file
system('git log --pretty=format:"%cd: (here I want the filename)"')


presented as sample.c for example.

I tried
#{filename}
but is interpreted as a string from the compiler and the result is the same as the input.

Thank you in advance.

Answer

It is interpreting it as a string because single quotes do now allow string interpolation.

system('git log --pretty=format:"%cd: (here I want the filename)"')

You can change this to use double quotes so you can take advantage of interpolation and escaping.

system("git log --pretty=format:\"%cd: #{filename}\"")

https://ruby-doc.org/core-2.1.1/doc/syntax/literals_rdoc.html

Comments