我使用ruby版本2.0.0,我在名为logo.txt的文本文件中制作了一些自定义徽标,如下所示:
_____
| |
|_____|
|
|
|
Run Code Online (Sandbox Code Playgroud)
现在我创建了一个名为"custom"的gem,并将此文件放在lib/logo.txt下.现在我想在ruby gem下的脚本中打印这个文件,所以我这样写了.
file = File.open("lib/logo.txt")
contents = file.read
puts "#{contents}"
Run Code Online (Sandbox Code Playgroud)
但上面的代码会产生错误,例如:
/usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/gems/2.0.0/gems/custom-0.0.1/lib/custom/custom.rb:1551:in `initialize': No such file or directory - lib/logo.txt (Errno::ENOENT)
Run Code Online (Sandbox Code Playgroud)
我在gemspec中包含了这个logo.txt文件,如下所示:
Gem::Specification.new do |s|
s.name = "custom"
s.version = VERSION
s.author = "Custom Wear"
s.email = "custom@custom.com"
s.homepage = "http://custom.com"
s.summary = "custom wera"
s.description = File.read(File.join(File.dirname(__FILE__), 'README'))
s.license = 'ALL RIGHTS RESERVED'
s.files = [""lib/custom.rb", "lib/custom/custom.rb", "lib/custom /version.rb","lib/logo.txt"]
s.test_files = Dir["spec/**/*"]
s.executables = [ 'custom' ]
s.require_paths << 'lib/'
Run Code Online (Sandbox Code Playgroud) 我使用ruby版本2.0.0,我有一个demo.json看起来像这样的文件:
{ "demo":
{
"rama" : { "Name": "demo" },
"krishna" : { "Name": "hare","place": "bharat", "hawa": { "maina": "tota"} }
}
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试通过这种方式操作json文件:
require 'json'
options = {}
options[:demo] = "kailash"
File.open("demo.json","w") do |f|
f.write(JSON.pretty_generate(options))
end
Run Code Online (Sandbox Code Playgroud)
我想替换一些值并在现有JSON文件中添加一些新的键值对,并且不想完全替换整个JSON文件.有没有办法做到这一点?