Ruby - 无法在简单程序中将字符串转换为 IO 错误
Ruby - Can't convert string to IO error in simple program
我正在尝试制作一个简单的 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]
我认为你的问题是你定义的方式method_missing
你没有在模块或class内部定义它,所以它是为主要对象定义的。
重写 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
我正在尝试制作一个简单的 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]
我认为你的问题是你定义的方式method_missing
你没有在模块或class内部定义它,所以它是为主要对象定义的。
重写 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