Search code examples
rubyjsongithub-linguist

How to convert a Ruby object to JSON from a File?


I'm trying to use the Linguist gem: https://github.com/github/linguist

My code is:

require 'linguist'

filePath = ARGV
langDetails = Linguist::FileBlob.new(filePath)
puts langDetails

That outputs: #<Linguist::FileBlob:0x007faf93b17200>

However, when I do puts langDetails.language, I get

/Users/myuser/.rvm/gems/ruby-1.9.3-p545@linguist/gems/github-linguist-2.10.15/lib/linguist/file_blob.rb:39:in `stat': can't convert Array into String (TypeError)
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p545@linguist/gems/github-linguist-2.10.15/lib/linguist/file_blob.rb:39:in `mode'
  from /Users/myuser/.rvm/gems/ruby-1.9.3-p545@linguist/gems/github-linguist-2.10.15/lib/linguist/blob_helper.rb:294:in `language'
  from ./linguist.rb:9:in `<main>'

I'm not entirely sure what I'm doing wrong. Ideally I want the data back as a JSON object. How do I accomplish this?


Solution

  • Look at the source. FileBlog is saying File.stat(@path).mode.to_s(8) but @path is an array. filePath needs to be a path string, but ARGV is an array.

    Perhaps you meant ARGV[0]?