【问题标题】:'Error: Cannot open "/home/<...>/billy-bones/=" for reading' while using pry and DataMapper'错误:使用 pry 和 DataMapper 时无法打开“/home/<...>/billy-bones/=" 进行阅读”
【发布时间】:2014-09-19 02:38:13
【问题描述】:

所以,我正在尝试为我的开发需求构建一个快速控制台程序,类似于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

如果我在控制台脚本中用 irb 代替 pry,就可以了。

非常感谢!

P. S.

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

P. P.S.

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

** 已解决 **

该死的你撬!

好的,这就是区别。

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

【问题讨论】:

    标签: ruby sinatra ruby-datamapper pry


    【解决方案1】:

    不回答我通常讨厌ruby 中的案例陈述的撬问题。

    为什么不改变:

    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
    

    【讨论】:

    • 感谢您的建议。我只是不确定什么是“更干净”:案例陈述或 self.send。
    • @art-solopov case 语句通常被认为是 ruby​​ 中的“代码异味”。抱歉,我无法解决真正的问题
    猜你喜欢
    • 2022-10-13
    • 2016-08-02
    • 1970-01-01
    • 2020-03-23
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    相关资源
    最近更新 更多