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

标签 ruby sinatra ruby-datamapper pry

因此,我正在尝试构建一个快速控制台程序来满足我的开发需求,类似于 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/

相关文章:

ruby - ==~ 运算符是做什么的?

ruby - 使用 Ruby Sinatra 创建单页代理

ruby - 如何使用带有 Ruby 的 Sinatra gem 隐藏 API key

ruby - 我如何用 sinatra 做 html 表单?

ruby - 如何从数组中生成数组的 Ruby 哈希

ruby - Windows 上的 Jekyll 无需安装

ruby-on-rails - 在 Ruby 中并发

javascript - 在 "vanilla"javascript 中发布 AJAX 请求,没有 jQuery,在 Rails 4 中

ruby - Datamapper:报告为什么我不能销毁记录

ruby-on-rails - 将原始 SQL 查询的结果转换为 JSON(和 CSV)