Suavocado
Suavocado

Reputation: 969

Ruby - Can't convert string to IO error in simple program

I'm trying to make a simple DSL and the following code works in returning an array of the "Pizza" in the console.

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

However, when I try to run tests using Rake, I get the following errors:

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" ]

This is PizzaBuilder_test.rb, I took out the tests to try and make it work but no luck.

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


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

This is the 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]

Upvotes: 0

Views: 68

Answers (1)

Josh
Josh

Reputation: 8596

I think your problem is the way you're defining method_missing you're not defining it inside of a module or class, so it's being defined for the main Object.

It's not a great idea to override method_missing, especially as a catch all like you did. I would recommend rewriting your code to use strings instead of overwriting method_missing.

Also your create method seems unnecessary. If you remove that the code below should work fine:

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

Upvotes: 1

Related Questions