-1
我试图用语言学家宝石:https://github.com/github/linguist如何将Ruby对象从文件转换为JSON?
我的代码是:
require 'linguist'
filePath = ARGV
langDetails = Linguist::FileBlob.new(filePath)
puts langDetails
输出:#<Linguist::FileBlob:0x007faf93b17200>
然而,当我做puts langDetails.language
,我得到
/Users/myuser/.rvm/gems/[email protected]/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/[email protected]/gems/github-linguist-2.10.15/lib/linguist/file_blob.rb:39:in `mode'
from /Users/myuser/.rvm/gems/[email protected]/gems/github-linguist-2.10.15/lib/linguist/blob_helper.rb:294:in `language'
from ./linguist.rb:9:in `<main>'
我不完全确定我做错了什么。理想情况下,我希望将数据作为JSON对象。我该如何做到这一点?
太好了!感谢您的发现。但是现在我怎样才能得到结果作为一个JSON对象? – Shamoon
您期望作为JSON对象得到什么结果?为什么?我从来没有使用过语言学家,但在文档中我没有看到任何有关这方面的知识。它会告诉你你的文件使用什么语言。这是一个字符串。你如何期待语言学家将你从“一个Ruby对象转换为JSON”是我的理解之外的。你得到一个错误,我解释它。 – matt
如果您想将Ruby对象转换为JSON,请使用'to_json'方法。但是,我不明白语言学家与它有什么关系。 – matt