/billy-bones/=" for reading' while using pry and DataMapper-6ren"> /billy-bones/=" for reading' while using pry and DataMapper-因此,我正在尝试构建一个快速控制台程序来满足我的开发需求,类似于 rails console(我正在使用 Sinatra + DataMapper + pry)。 我运行它并启动 cat = Cate-6ren">
gpt4 book ai didi

ruby - '错误 : Cannot open "/home/<...>/billy-bones/=" for reading' while using pry and DataMapper

转载 作者:数据小太阳 更新时间:2023-10-29 08:12:15 24 4
gpt4 key购买 nike

因此,我正在尝试构建一个快速控制台程序来满足我的开发需求,类似于 rails console(我正在使用 Sinatra + DataMapper + pry)。

我运行它并启动 cat = Category.new(name: 'TestCat', type: :referential)。它给了我以下错误:

Error: Cannot open "/home/art-solopov/Projects/by-language/Ruby/billy-bones/=" for reading.

问题的原因可能是什么?

控制台:

#!/usr/bin/env ruby
$LOAD_PATH << 'lib'

require 'pry'
require 'config'

binding.pry

lib/config.rb:

# Configuration files and app-wide requires go here

require 'sinatra'
require 'data_mapper'
require 'model/bill'
require 'model/category'
configure :production do
DataMapper::Logger.new('db-log', :debug)
DataMapper.setup(:default,
'postgres://billy-bones:billy@localhost/billy-bones')
DataMapper.finalize
end

configure :development do
DataMapper::Logger.new($stderr, :debug)
DataMapper.setup(:default,
'postgres://billy-bones:billy@localhost/billy-bones-dev')
DataMapper.finalize
DataMapper.auto_upgrade!
end

configure :test do
require 'dm_migrations'
DataMapper::Logger.new($stderr, :debug)
DataMapper.setup(:default,
'postgres://billy-bones:billy@localhost/billy-bones-test')
DataMapper.finalize
DataMapper.auto_migrate!
end

lib/model/category.rb:

require 'data_mapper'

class Category
include DataMapper::Resource

property :id, Serial
property :name, String
property :type, Enum[:referential, :predefined, :computable]

has n, :bills
# has n, :tariffs TODO uncomment when tariff ready

def create_bill(params)
# A bill factory for current category type
case type
when :referential
ReferentialBill.new params
when :predefined
PredefinedBill.new params
when :computable
ComputableBill.new params
end
end

end

如果我在控制台脚本中将 pry 替换为 irb,一切正常。

非常感谢!

P. S.

好的,昨天我再次尝试了这个脚本,它运行得很好。我没有更改任何内容。我不确定现在是否应该删除问题。

P. P.S.

或者其实不是……今天又遇到了。仍然完全不知道可能导致它的原因。

** 已解决 **

你他妈的 pry !

好的,这就是区别。

当我第二次测试它时,我实际上输入了 a = Category.new(name: 'TestCat', type: :referential) 并且它起作用了。看起来 pry 只是认为 cat 是一个 Unix 命令,而不是一个有效的变量名。

最佳答案

不回答窥探问题我只是通常讨厌 ruby 中的 case 语句。

为什么不改变:

def create_bill(params)
# A bill factory for current category type
case type
when :referential
ReferentialBill.new params
when :predefined
PredefinedBill.new params
when :computable
ComputableBill.new params
end
end

到:

def create_bill(params)
# A bill factory for current category type
self.send("new_#{type}_bill",params)
end
def new_referential_bill(params)
ReferentialBill.new params
end
def new_predefined_bill(params)
PredefinedBill.new params
end
def new_computable_bill(params)
ComputableBill.new params
end

你可以让它更动态,但我认为在这种情况下这会降低可读性,但如果你喜欢在 rails 中,这应该可以解决问题

def create_bill(params)
if [:referential, :predefined, :computable].include?(type)
"#{type}_bill".classify.constantize.new(params)
else
#Some Kind of Handling for non Defined Bill Types
end
end

或者这将在 rails 内部或外部工作

def create_bill(params)
if [:referential, :predefined, :computable].include?(type)
Object.const_get("#{type.to_s.capitalize}Bill").new(params)
else
#Some Kind of Handling for non Defined Bill Types
end
end

关于ruby - '错误 : Cannot open "/home/<...>/billy-bones/=" for reading' while using pry and DataMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24986252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com