2015-10-29 2 views
0

Я пытаюсь сделать простой DSL, и следующий код работает при возврате массива «Пицца» в консоли.Ruby - Невозможно преобразовать строку в ошибку ввода-вывода в простую программу

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] 
+0

Пожалуйста, ваши тестовый код, в частности, 'Тест/PizzaBuilder_test.rb' – Josh

+0

@Josh Я просто добавил все соответствующие файлы, есть на самом деле не что-нибудь интересное в тестовый файл, кроме канарейского теста. Спасибо, что посмотрели. – Suavocado

ответ

1

Я думаю, ваша проблема в том, как вы определяете 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