[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Mexican Blanket (#127) Problem

Ari Brown

6/17/2007 11:00:00 PM

Hey all,
I was attempting Mexican Blanket today, and found it to be more of a
brain teaser (for me) than a coding problem.

Which doesn't mean there were no coding problems.

I keep getting an error for the Integer#times method. I think I'm
using it right, but could someone help me correct it please?

Also, do I have my shebang setup correctly to type ./
mexican_blanket.rb ETC and have it run? Because I can never seem to
get it to work.

------------------------------------------------------------------|
Error
------------------------------------------------------------------|
mexican_blanket.rb:21:in `weave': undefined method `times' for
"25":String (NoMethodError)
from mexican_blanket.rb:15:in `initialize'
from mexican_blanket.rb:49:in `new'
from mexican_blanket.rb:49

------------------------------------------------------------------|
Code
------------------------------------------------------------------|
#!/usr/bin/ruby
#this one is just a test thing. Not gonna do the designs diagonally

class Blanket

def initialize(colors, length, width, pattern_length)
@COLORS = colors.split(//)
@LENGTH = length
@WIDTH = width
@slot = -1
@SLOTS = @COLORS.length
@PL = pattern_length
@mainp = pattern_length
@offp = 1
weave()
end

def weave() # THIS IS LINE 15
@LENGTH.times do |i|
@mainp.times { #build the PATTERN long...
@slot = (@slot + 1)
width_ticker = 0
until width_ticker == @WIDTH #build it wide...
print '#{@COLOR[(@slot % @SLOTS)]}'
width_ticker = width_ticker + 1
ticker = ticker + 1
end
}
end

@offp.times do |i|
@slot = (@slot + 1)
width_ticker = 0
until width_ticker == @WIDTH #build it wide...
print '#{@COLOR[(@slot % @slots)]}'
width_ticker = width_ticker + 1
ticker = ticker + 1
end
end
@mainp = (@mainp + (@PL - 1)) % @PL
@offp = @offp.next % @PL
end
end

colors = ARGV[0]
length = ARGV[1]
width = ARGV[2]
pattern_length = ARGV[3]
mexico = Blanket.new(colors, length, width, pattern_length) #THIS
IS LINE 49
#end
-------------------------------------------|
~aRi
I did my version of it as a top-down pattern as opposed to a diagonal
pattern.



1 Answer

Harry Kakueki

6/18/2007 1:01:00 AM

0

On 6/18/07, Ari Brown <ari@aribrown.com> wrote:
> Hey all,
> I was attempting Mexican Blanket today, and found it to be more of a
> brain teaser (for me) than a coding problem.
>
> Which doesn't mean there were no coding problems.
>
> I keep getting an error for the Integer#times method. I think I'm
> using it right, but could someone help me correct it please?
>
> ------------------------------------------------------------------|
> mexican_blanket.rb:21:in `weave': undefined method `times' for
> "25":String (NoMethodError)
> from mexican_blanket.rb:15:in `initialize'
> from mexican_blanket.rb:49:in `new'
> from mexican_blanket.rb:49
>
> ------------------------------------------------------------------|

It looks like "25" is a String. You need to change it to an Integer.

colors = ARGV[0]
length = ARGV[1]
width = ARGV[2]
pattern_length = ARGV[3]
p width.class # Add this line and take a look

mexico = Blanket.new(colors, length, width, pattern_length)

You may want to check the others, too.

Harry

--

A Look into Japanese Ruby List in English
http://www.ka...