我正在尝试使用以下代码在终端中接受文件作为参数,然后读取该body
参数并使用其内容更新变量。如果文件未传入,那么我希望提示用户可以输入自己的正文副本。
require 'posterous'
Posterous.config = {
'username' => 'name',
'password' => 'pass',
'api_token' => 'token'
}
include Posterous
@site = Site.primary
#GETS POST TITLE
puts "Post title: "
title = STDIN.gets.chomp()
if defined?(ARGV)
filename = ARGV.first
end
if (defined?(filename))
body = File.open(filename)
body = body.read()
else
puts "Post body: "
body = STDIN.gets.chomp()
end
puts body
当我在不传递文件的情况下运行程序时,我得到了这个返回:
Post title:
Hello
posterous.rb:21:in `initialize': can't convert nil into String (TypeError)
from posterous.rb:21:in `open'
from posterous.rb:21:in `'
我对红宝石相当陌生,因此不是最擅长的。我试过交换很多东西并改变东西,但无济于事。我究竟做错了什么?