librelist archives

« back to archive

Logic Coding

Logic Coding

From:
DeAndrea Monroe
Date:
2010-03-25 @ 20:24
I have two different logic files that I am trying to combine and get to run
properly. Everytime I run the test2.rb file, I get an error in line 17. What
could be the problem?

*test2.rb*

require 'thing2'
require 'test/unit'
class TestThing2 < Test::Unit::TestCase
   include Thing
   def setup
      @array = []
       setarray
   end
   def test_length
     puts @array.length
     assert @array.length == 4
    end
   def test_legal?
     assert legal?(0,3)
     assert !legal?(1,2)
     assert !legal?(1,4)
     assert !legal?(1,3)
     assert legal?(2,3)
   end

   def setarray
      for a in (0...4)
         @array[a] = :empty
       end
    end
end


*thing2.rb*
**
module Thing
   def legal?(from, to)
      if @array[from] == :empty
        return false
      elsif @array[to] != :empty
        return false
      elsif @array[from] != :empty
        return false
      elsif @array[from]%2 == @array[to]%3
        return false
      else
        return true
      end
   end
end

Re: [shoes] Logic Coding

From:
Devyn Cairns
Date:
2010-03-26 @ 08:50
Again, use a pastebin, please? Also, this is not Shoes-related, this is
strictly Ruby-related. Since I'm nice, I'll help anyway :D

On Thu, Mar 25, 2010 at 1:24 PM, DeAndrea Monroe <dmonroe4919@gmail.com>wrote:

> I have two different logic files that I am trying to combine and get to run
> properly. Everytime I run the test2.rb file, I get an error in line 17. What
> could be the problem?
>
> *test2.rb*
>
> require 'thing2'
> require 'test/unit'
> class TestThing2 < Test::Unit::TestCase
>    include Thing
>    def setup
>       @array = []
>        setarray
>    end
>    def test_length
>      puts @array.length
>      assert @array.length == 4
>     end
>    def test_legal?
>      assert legal?(0,3)
>      assert !legal?(1,2)
>      assert !legal?(1,4)
>      assert !legal?(1,3)
>      assert legal?(2,3)
>    end
>
>    def setarray
>       for a in (0...4)
>          @array[a] = :empty
>        end
>     end
> end
>
>
> *thing2.rb*
> **
> module Thing
>    def legal?(from, to)
>       if @array[from] == :empty
>         return false
>       elsif @array[to] != :empty
>         return false
>       elsif @array[from] != :empty
>         return false
>       elsif @array[from]%2 == @array[to]%3
>         return false
>       else
>         return true
>       end
>    end
> end
>
>
>
>
>
>
>

First of all, the error you're experiencing just means that a test failed
(in this case, the 'assert legal?(0,3)'). Also, if I understand correctly,
every element of the array will be :empty. You specifically stated that if
the value of @array[from] == :empty, the test shall fail. Also, just a note,
@array[from]%2 means, the modulo of @array[from] and 2.
http://en.wikipedia.org/wiki/Modulo. The usage seems a little strange there,
that's just why I was pointing it out.


-- 
   ~devyn