Character spacing in ls-tree command output (Ruby)

I’m stuck on Stage #kp1 Read a Tree Object. Well, not quite stuck, but I have an issue with the output.

Here is my output:

> ruby app/main.rb ls-tree ff6701c8e530eba2971947371969961b452a262b
040000 tree ee82ed095fba59cc4813fe531c34d0c902e1c    .codecrafters
100644 blob 176a458f94e0ea5272ce67c36bf3b6be9caf623  .gitattributes
100644 blob f5f8981fa3cf5b9354bbe3201a7ad0517c73b8   Gemfile
100644 blob 5bcfb0539d67a86ebfdf012718b223861b92346  Gemfile.lock
100644 blob f2c66328f79f85f856079812a55b64ac6170b4   README.md
040000 tree 31a982d7bbc82617594409b7a2ef9fe3fd3cd69  app
100644 blob ddced257417d3c76a282e412c4931f2b2d0887d  codecrafters.yml
100644 blob 685a6166843fa19d48fafb9b8829fa1bbd74d73  newfile
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 testfile
100755 blob 6751f3a8e1f5cdfc43bad7c1aec91729865ff3d6 your_program.sh

And here is the output of the actual git command:

> git ls-tree ff6701c8e530eba2971947371969961b452a262b
040000 tree ee820ed095fba59cc48103fe531c34d0c9002e1c    .codecrafters
100644 blob 176a458f94e0ea5272ce67c36bf30b6be9caf623    .gitattributes
100644 blob f5f89801fa3cf5b9354b0be3201a7ad0517c73b8    Gemfile
100644 blob 5bcfb0539d067a86ebfdf012718b223861b92346    Gemfile.lock
100644 blob f2c663028f79f85f8560790812a55b64ac6170b4    README.md
040000 tree 31a9820d7bbc82617594409b7a2ef9fe3fd3cd69    app
100644 blob ddce0d257417d3c76a282e412c4931f2b2d0887d    codecrafters.yml
100644 blob 685a6166843fa19d48fa0fb9b8829fa1bbd74d73    newfile
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    testfile
100755 blob 6751f3a8e1f5cdfc43bad7c1aec91729865ff3d6    your_program.sh

I would really like to have the output nicely aligned too!

Here is my code for formatting the output:

entries << if mode =~ /.40000/
      '040000 tree %-40<sha>s %<name>s' % {name: name, sha: bin_to_hex(sha)}
    else
      '%<mode>s blob %-40<sha>s %<name>s' % {mode: mode, sha: bin_to_hex(sha), name: name}
    end

But as shown above, that does not align the hashes as desired. Any help would be greatly appreciated.

Hey @one-ant, looks like you’ve got past this stage. Do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

Thanks for following up. Actually, I still have this alignment problem, but the tester does not mind, so I started working on the next stage. I would still love to find a solution!

@one-ant Could you share the commit hash of your code for handling the non-“name-only” case? In the latest commit, nothing seems to get printed.

The latest one should be fine… Here is the output I get:

 ruby app/main.rb ls-tree ff6701c8e530eba2971947371969961b452a262b
040000 tree ee82ed095fba59cc4813fe531c34d0c902e1c    .codecrafters
100644 blob 176a458f94e0ea5272ce67c36bf3b6be9caf623  .gitattributes
100644 blob f5f8981fa3cf5b9354bbe3201a7ad0517c73b8   Gemfile
100644 blob 5bcfb0539d67a86ebfdf012718b223861b92346  Gemfile.lock
100644 blob f2c66328f79f85f856079812a55b64ac6170b4   README.md
040000 tree 31a982d7bbc82617594409b7a2ef9fe3fd3cd69  app
100644 blob ddced257417d3c76a282e412c4931f2b2d0887d  codecrafters.yml
100644 blob 685a6166843fa19d48fafb9b8829fa1bbd74d73  newfile
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 testfile
100755 blob 6751f3a8e1f5cdfc43bad7c1aec91729865ff3d6 your_program.sh
> ruby app/main.rb ls-tree --name-only ff6701c8e530eba2971947371969961b452a262b
.codecrafters
.gitattributes
Gemfile
Gemfile.lock
README.md
app
codecrafters.yml
newfile
testfile
your_program.sh

@one-ant Would you mind pushing your latest code? The most recent commit on our server is from a week ago, and it looks like this:

There should now be a commit with your name on it. :grinning_face:

Got it! :+1:

Ensuring bin_to_hex formats bytes as two-digit hex will give you the desired output:

Awesome! Thank you so much!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.