RE: Ruby code problem

  • From: "Macarty, Jay {PBSG}" <Jay.Macarty@xxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 16 Jan 2008 10:17:51 -0600

I have noticed the same issue with ruby console programs and jaws. Don't
know what happens with that. 
 

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jamal Mazrui
Sent: Tuesday, January 15, 2008 9:49 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: Ruby code problem

Here is my effort at trying to improve the example.  I noticed that the
code proposed on the Ruby list prompted for row and column values
without
informing the user what data is sought.  I also noticed that it did not
print the resulting dimensions and contents, which the previous code
did.
My code below addresses these points and avoids defining a separate
function.  Oddly, JAWS goes silent during the prompting phase, but
subsequently reads the console screen after the program runs.

Jamal

# Prompt user for number of rows and columns
puts "Enter number of rows:"
n_row = gets.to_i
puts "Enter number of columns:"
n_col = gets.to_i

# Create the table with a random number below 10 in each cell
table = Array.new(n_row) {Array.new(n_col) {rand(10)}}

# Verify the dimensions
puts "Table has #{n_row} rows and #{n_col} columns"

# Verify the contents, seperating each row by a line and each column by
a space
table.each {|row| puts row.join(" ")}


On Tue, 15
Jan 2008, Sean
Murphy wrote:

> Date: Tue, 15 Jan 2008 20:23:23 +1100
> From: Sean Murphy <smurf_bp@xxxxxxxxxx>
> Reply-To: programmingblind@xxxxxxxxxxxxx
> To: programmingblind@xxxxxxxxxxxxx
> Subject: Re: Ruby code problem
>
> Hi,
>
> thanks for the response.  I found the problem in the original code.  I
was
> not re-initialising the col array.  The below code is how I should be
doing
> my example:
>
> def make_2d_array n_row, n_col
>   Array.new(n_row) do
>     Array.new(n_col){ rand(2) }
>   end
> end
>
> make_2d_array(gets.to_i, gets.to_i)
>
>
> The "  Array.new(n_row) do
> " creates the primary array and sets up a code block.  Depending on
the
> number of elements I want to create for Row determines how many times
the
> code block is executed.  The line which creates the col array, does
the same
> thing as previously described and executes the random method code
block.
> The value from the Random method is assigned to the new element for
col.
> Once all the columns are created, the col array is assigned to the row
> array.  Then the whole process is repeated until all the number of
elements
> defined to row have been created.
>
> I hope this helps others and I didn't come up with the solution
either.  I
> went to the Ruby mail list and receive a good answer from there.
>
> Regards
> Sean Murphy
> Skype: smurf20005
>
> Life is a challenge, treat it that way.
> ----- Original Message -----
> From: "Jamal Mazrui" <empower@xxxxxxxxx>
> To: <programmingblind@xxxxxxxxxxxxx>
> Sent: Tuesday, January 15, 2008 3:22 AM
> Subject: Re: Ruby code problem
>
>
> > Try moving the line
> >  row = Array.new
> > below the line
> >  while (counter_x < x)
> >
> > I think the row array continued to grow instead of being
re-initialized
> > for each column array.  Each item in the column array contained a
> > reference to the same row array, so their values were the same.
> >
> > Jamal
> >
> > On Sun, 13 Jan 2008, Sean Murphy wrote:
> >
> >> Date: Sun, 13 Jan 2008 16:58:30 +1100
> >> From: Sean Murphy <smurf_bp@xxxxxxxxxx>
> >> Reply-To: programmingblind@xxxxxxxxxxxxx
> >> To: programmingblind@xxxxxxxxxxxxx
> >> Subject: Ruby code problem
> >>
> >> All,
> >>
> >> I am trying to create a two dimension array in Ruby.  The books I
have
> >> referred two doesn't give me any examples on how to create a two
> >> dimension
> >> array by a programmatic way.  They do talk about creating it by
hard
> >> code.
> >>
> >> The below method is trying creates a 3 by 3 array with a random
number
> >> between 0 to 1.
> >>
> >> def create_world x, y
> >>   col = Array.new
> >>   row = Array.new
> >>   counter_x = 0
> >>   counter_y = 0
> >>
> >>   while (counter_x < x)
> >>   puts 'x ' + counter_x.to_s.to_s
> >>     while (counter_y < y)
> >>       puts 'y ' + counter_y.to_s
> >>       map_type = rand(2)
> >>       puts 'Random: ' + map_type.to_s
> >>       row.push map_type
> >>       counter_y = counter_y + 1
> >>   end
> >>   col.push row
> >>   counter_y = 0
> >>   counter_x = counter_x + 1
> >>   end
> >>   return col
> >> end
> >>
> >> world = create_world(3, 3)
> >> puts world.to_s
> >> puts 'array ' + world[0].size.to_s
> >>
> >> Problem with code above is:
> >>
> >> 1.  I get 3 rows.  But in each row I get 9 columns.
> >> 2.  Each row content is the same.
> >>
> >> So can anyone help with the above?
> >>
> >> Regards
> >> Sean Murphy
> >> Skype: smurf20005
> >>
> >> Life is a challenge, treat it that way.
> >>
> >> __________
> >> View the list's information and change your settings at
> >> //www.freelists.org/list/programmingblind
> >>
> > __________
> > View the list's information and change your settings at
> > //www.freelists.org/list/programmingblind
> >
>
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: