Ruby - 在简单程序中无法将字符串转换为IO错误

时间:2015-10-29 01:50:30

标签: ruby dsl

我试图制作一个简单的DSL,并且以下代码可用于返回" Pizza"在控制台中。

class PizzaIngredients 
    def initialize 
        @@fullOrder = []
    end

    #this takes in our toppings and creates the touple
    def spread (topping)
        @@fullOrder << "Spread #{topping}"
    end

    def bake
        @@fullOrder 
    end

    #this handles whatever is not a spread and is expected to take in a list in the format topping top1, top2, top3 and so on
    def toppings (*toppingList)
        array = []
        toppingList.each {|topping| array << "topping #{topping}"}

        array.each {|iter| @@fullOrder << iter}
    end
end

# hadels if any methods are missing
def method_missing(name, *args, &block)
    "#{name}"
end

#this is our entry into the dsl or manages it
module Pizza #smokestack
    #this keeps a list of your order preserving the order in which the components where added
    @@order = []
    def self.create(&block)
        if block_given?
            pizza = PizzaIngredients.new
            @@order << pizza.instance_eval(&block)
        else
            puts "making the pizza with no block"           
        end

    end



end

def create (ingnore_param, &block)
    Pizza.create(&block)

end

create pizza do 
    spread cheese
    spread sauce
    toppings oregano, green_pepper, onions, jalapenos
    spread sauce
    bake
end

但是,当我尝试使用Rake运行测试时,我收到以下错误:

C:/Ruby21-x64/bin/ruby.exe -w -I"lib" -I"C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib" "C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb" "test/PizzaBuilder_test.rb"
C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/test-unit-3.0.9/lib/test/unit/autorunner.rb:142:in `exist?': can't convert String to IO (String#to_io gives String) (TypeError)
        from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/test-unit-3.0.9/lib/test/unit/autorunner.rb:142:in `initialize'
        from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/test-unit-3.0.9/lib/test/unit/autorunner.rb:55:in `new'
        from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/test-unit-3.0.9/lib/test/unit/autorunner.rb:55:in `run'
        from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/test-unit-3.0.9/lib/test/unit.rb:502:in `block (2 levels) in <top (required)>'
rake aborted!
Command failed with status (1): [ruby -w -I"lib" -I"C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib" "C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb" "test/PizzaBuilder_test.rb" ]

这是PizzaBuilder_test.rb,我拿出测试试图让它工作但没有运气。

require "test/unit"
require "./main/PizzaBuilder"


class TestMyApplication < Test::Unit::TestCase
    def testCase
        assert(true, "dummy case failed")
    end
end 

这是Rakefile:

require 'rake'
require 'rake/testtask'

Rake::TestTask.new do |t|
      t.libs = ["lib"]
      t.warning = true
      t.verbose = true
      t.test_files = FileList['test/*_test.rb']
end

task default:[:test]

1 个答案:

答案 0 :(得分:1)

我认为你的问题是你定义method_missing的方式,你没有在模块或类中定义它,所以它是为主Object定义的。

重写method_missing并不是一个好主意,特别是像你一样捕获所有内容。我建议重写代码以使用字符串而不是覆盖method_missing。

您的创建方法似乎也没必要。如果你删除,下面的代码应该可以正常工作:

class PizzaIngredients 
    def initialize 
        @@fullOrder = []
    end

    #this takes in our toppings and creates the touple
    def spread (topping)
        @@fullOrder << "Spread #{topping}"
    end

    def bake
        @@fullOrder 
    end

    #this handles whatever is not a spread and is expected to take in a list in the format topping top1, top2, top3 and so on
    def toppings (*toppingList)
        array = []
        toppingList.each {|topping| array << "topping #{topping}"}

        array.each {|iter| @@fullOrder << iter}
    end
end

#this is our entry into the dsl or manages it
module Pizza #smokestack
    #this keeps a list of your order preserving the order in which the components where added
    @@order = []
    def self.create(&block)
        if block_given?
            pizza = PizzaIngredients.new
            @@order << pizza.instance_eval(&block)
        else
            puts "making the pizza with no block"           
        end
    end
end

Pizza.create do 
    spread 'cheese'
    spread 'sauce'
    toppings 'oregano', 'green pepper', 'onions', 'jalapenos'
    spread 'sauce'
    bake
end