# Logic

**URL:** <https://www.ruby-forum.com/t/logic/97111>\
**Category:** Ruby\
**Created:** [December 10, 2005, 6:25pm UTC](https://www.ruby-forum.com/t/logic/97111 "2005-12-10T18:25:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 6:25pm UTC](https://www.ruby-forum.com/t/logic/97111/1 "2005-12-10T18:25:40Z")

</div>

def MakeBoard  
rcount = 0  
puts “Push enter, then begin input. (For Input Format Compliance, extra  
enter key is necessary)”  
gets  
board = [[],[],[],[],[],[],[],[],[]]  
9.times do  
instr = gets  
board[rcount] =  
[instr[0].to\_i,instr[1].to\_i,instr[2].to\_i,instr[3].to\_i,instr[4].to\_i,instr[5].to\_i,instr[6].to\_i,instr[7].to\_i,instr[8].to\_i]  
rcount += 1  
puts board[rcount]  
end #do  
return board  
end #MakeBoard

Is my logic flawed here? the puts doesnt actually output anything.

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 6:39pm UTC](https://www.ruby-forum.com/t/logic/97111/2 "2005-12-10T18:39:05Z")

</div>

StarLion wrote:

> Is my logic flawed here? the puts doesnt actually output anything.

It is supposed not to print anything since nothing is called here…

just call the MakeBoard method

lopex

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 6:43pm UTC](https://www.ruby-forum.com/t/logic/97111/3 "2005-12-10T18:43:19Z")

</div>

Ok, i made a bit more cleaned up version, but i’m still not sure what  
you want  
to do with your method.  
especially, what does this .to\_i mean?

However, for everything you write twice or more often, ruby usually has  
a way  
to make it at the same time cleaner and easier to write.

def make\_board  
puts “Push enter, then begin input.”  
puts “(For Input Format Compilance, extra enter key is necessary)”  
gets  
board = Array.new(9){[]}  
board.size.times do |i|  
instr = gets  
board[i] = instr.split(//).map{|c| c.to\_i}  
puts board[i]  
end  
return board  
end

Am Samstag, 10. Dezember 2005 18:25 schrieb StarLion:

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 6:43pm UTC](https://www.ruby-forum.com/t/logic/97111/4 "2005-12-10T18:43:22Z")

</div>

StarLion wrote:

> rcount += 1  
> puts board[rcount]  
> end #do  
> return board  
> end #MakeBoard
> 
> Is my logic flawed here? the puts doesnt actually output anything.

I think it’s because when you write `puts board[rcount]',`rcount’ is 1  
higher than the highest index number in board. Try this instead.

def make\_board  
puts “Push enter, then begin input. (For Input Format Compliance,  
extra enter key is necessary)”  
gets  
board = [[],[],[],[],[],[],[],[],[]]  
9.times do |i|  
# you may want to use gets.chomp instead  
instr = gets  
board[i] =  
[instr[0].to\_i,instr[1].to\_i,instr[2].to\_i,instr[3].to\_i,instr[4].to\_i,instr[5].to\_i,instr[6].to\_i,instr[7].to\_i,instr[8].to\_i]  
puts board[i] # the last item in `board’  
end  
return board  
end

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 6:55pm UTC](https://www.ruby-forum.com/t/logic/97111/5 "2005-12-10T18:55:37Z")

</div>

m.fellinger wrote:

> Ok, i made a bit more cleaned up version, but i’m still not sure what  
> you want  
> to do with your method.  
> especially, what does this .to\_i mean?
> 
> However, for everything you write twice or more often, ruby usually has  
> a way  
> to make it at the same time cleaner and easier to write.
> 
> def make\_board  
> puts “Push enter, then begin input.”  
> puts “(For Input Format Compilance, extra enter key is necessary)”  
> gets  
> board = Array.new(9){[]}  
> board.size.times do |i|  
> instr = gets  
> board[i] = instr.split(//).map{|c| c.to\_i}  
> puts board[i]  
> end  
> return board  
> end
> 
> Am Samstag, 10. Dezember 2005 18:25 schrieb StarLion:

The idea is to take a string of 9 characters, and transform them into an  
array of integers, which are stored in an array of rows (IE: generate a  
9x9 array.).

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 6:51pm UTC](https://www.ruby-forum.com/t/logic/97111/6 "2005-12-10T18:51:30Z")

</div>

On 12/10/05, StarLion [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rcount += 1  
> puts board[rcount]  
> end #do  
> return board  
> end #MakeBoard
> 
> Is my logic flawed here? the puts doesnt actually output anything.

I think this is a little cleaner:

make\_board returns a board split by spaces.

i.e. you enter the rows like

1 0 1 0 2 1 8 2 1  
1 2 1 1 9 1 9 1 9  
…

## The method returns the actual array, and I map and join for pretty printing in the puts. Tweak as needed, code below.

def make\_board  
board = []  
puts “input rows, seperated by spaces:”  
9.times { board \<\< gets.split(" ")[0…8].map { |e| e.to\_i } }  
return board  
end

puts make\_board.map { |r| r.join(" “) }.join(”\n")

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 7:07pm UTC](https://www.ruby-forum.com/t/logic/97111/7 "2005-12-10T19:07:44Z")

</div>

On 12/10/05, StarLion [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \<hmrhouse@hotmail.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > The idea is to take a string of 9 characters, and transform them into an  
> > array of integers, which are stored in an array of rows (IE: generate a  
> > 9x9 array.).
> 
> Obviously i meant 9 strings of 9 characters.

That is what my code does, but it seperates by spaces to make it  
easier to see as your typing in… replace the split(" ") with  
split(//) and it will do exactly that

rows can then be entered like this: 123456789

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 6:56pm UTC](https://www.ruby-forum.com/t/logic/97111/8 "2005-12-10T18:56:36Z")

</div>

> The idea is to take a string of 9 characters, and transform them into an  
> array of integers, which are stored in an array of rows (IE: generate a  
> 9x9 array.).

Obviously i meant 9 strings of 9 characters.

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 7:07pm UTC](https://www.ruby-forum.com/t/logic/97111/9 "2005-12-10T19:07:45Z")

</div>

i see… it wasn’t clear at first what the kind of data is that is the  
input  
for the program.  
However, after testing my little suggestion, i found that there are is a  
bug  
in it - it didn’t strip the \n from the line it gets. also i made the  
iteration independent from the size of the board and made the output  
each  
line a bit prettier ^^

def make\_board  
Â puts “Push enter, then begin input.”  
Â puts “(For Input Format Compilance, extra enter key is necessary)”  
Â gets  
Â board = Array.new(9){[]}  
Â 9.times do |i|  
Â Â instr = gets.strip  
Â Â board[i] = instr.split(//).map{|c| c.to\_i}  
Â Â puts board[i].inspect  
Â end  
Â return board  
end

Am Samstag, 10. Dezember 2005 18:56 schrieb StarLion  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
\<hmrhouse@hotmail.:

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 7:11pm UTC](https://www.ruby-forum.com/t/logic/97111/10 "2005-12-10T19:11:49Z")

</div>

On 12/10/05, Michael F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i see… it wasn’t clear at first what the kind of data is that is the input  
> for the program.  
> However, after testing my little suggestion, i found that there are is a bug  
> in it - it didn’t strip the \n from the line it gets. also i made the  
> iteration independent from the size of the board and made the output each  
> line a bit prettier ^^

if you use a range, you don’t need to worry about extraneous input  
(such as the \n)

def make\_board  
board = []  
puts “input rows, seperated by spaces:”  
9.times { board \<\< gets.split(//)[0…8].map { |e| e.to\_i } }  
return board  
end

puts make\_board.map { |r| r.join(" “) }.join(”\n")

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 10:52pm UTC](https://www.ruby-forum.com/t/logic/97111/11 "2005-12-10T22:52:35Z")

</div>

> This code returns a 10 element array, with 0 in index 9.

Nevermind… I just cant count how many characters i put on one line.  
-\_-

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 10:51pm UTC](https://www.ruby-forum.com/t/logic/97111/12 "2005-12-10T22:51:03Z")

</div>

> def make\_board  
> puts “Push enter, then begin input.”  
> puts “(For Input Format Compilance, extra enter key is necessary)”  
> gets  
> board = Array.new(9){[]}  
> 9.times do |i|  
> instr = gets.strip  
> board[i] = instr.split(//).map{|c| c.to\_i}  
> puts board[i].inspect  
> end  
> return board  
> end

This code returns a 10 element array, with 0 in index 9.

---

<div class="post-metadata">

**Author:** ![StarLion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@StarLion](https://www.ruby-forum.com/u/StarLion)\
**Post date:** [December 10, 2005, 11:03pm UTC](https://www.ruby-forum.com/t/logic/97111/13 "2005-12-10T23:03:55Z")

</div>

On Dec 10, 2005, at 3:52 PM, StarLion wrote:

> > This code returns a 10 element array, with 0 in index 9.
> 
> Nevermind… I just cant count how many characters i put on one line.

Which is probably a sign that input error checking is needed:

input =~ /^\d{9}$/

James Edward G. II
