3 Replies - 560 Views - Last Post: 22 June 2011 - 12:29 PM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Loosing track of my var types in arrays (ruby debugging tips)

Posted 22 June 2011 - 11:35 AM

Hey, I'm aiming for rails development, but I decided to get to know ruby a little better, and so I'm working on the sudoku challenge, and I keep winding up with arrays of numbers... but somehow they become an array of one slot, which contains an array of my numbers. This has happened on several occasions, and I can't even figure out why it's happening.

Also, there's gotta be a better way of setting up 2D arrays in ruby, so any advice on that would be appreciated.


Here's how I'm defining 2D arrays:
      @virtually_encumbered_columns = Array.new
      (0..2).each do |i|
        @virtually_encumbered_columns[i] = Array.new
      end



Pretty wordy for a simple matrix of length 3 (and no specific width :censored: ...)


I can't explain my problem concisely enough, and the code is a complete mess (poor choice of classes during planning :online2long: ) But basicaly, I try to do something like puts the class of my array, to verify that it's an array, then I get the class of the first index of that array, to verify it's a "fixnum" or a "string" (which are CONSTANTLY getting switched around in my code, I feel like I have no control in this language). But when I get the first index of my array, instead of being a fixnum, it's another array! Does anyone have any advice on how to sort out arrays and dubug a ruby app?

Is This A Good Question/Topic? 0
  • +

Replies To: Loosing track of my var types in arrays (ruby debugging tips)

#2 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Loosing track of my var types in arrays (ruby debugging tips)

Posted 22 June 2011 - 11:45 AM

And also, get a load of this problem:

      @regions[sq_row(y,x)].plausible_slots[sq_col(y,x)][0].delete(solution.to_s)    # I get: undefined method `delete' for nil:NilClass (NoMethodError)


      if x==4 && y == 5  # this is my version of a working debugger on netbeans/windows...
        puts "YELPLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"

        20.times do
          puts
        end
        puts solution.class
        puts @regions[sq_row(y,x)].plausible_slots[sq_col(y,x)].class        # => array
        puts @regions[sq_row(y,x)].plausible_slots[sq_col(y,x)][0].class     # => array!!!
        puts @regions[sq_row(y,x)].plausible_slots[sq_col(y,x)][0][0].class  # => fixnum
        @regions[sq_row(y,x)].plausible_slots[sq_col(y,x)][0].delete(9)      # => Always works fine when working on this index (4,5)


        puts @regions[sq_row(y,x)].plausible_slots[sq_col(y,x)][0]
        Process.exit

      end




I wish I could bold those two identical calls @regions[sq_row(y,x)].plausible_slots[sq_col(y,x)][0].delete(solution.to_s)


Edit: Ok, it looks like it's an empty array, and empty arrays count as nil, and throw the error... hmm...

Ack! Down the line, I accidentally feed plausible_slots[i] an array

          ret = self.basic_checker(row,col)      # retuns an array
          #puts ret
          
          @regions[sq_row(col,row)].plausible_slots[sq_col(col,row)] << ret



When I should have feed it each number of ret manually... Not sure what the best way to do that is other than looping =/

          ret.each do |plausibles|
            @regions[sq_row(col,row)].plausible_slots[sq_col(col,row)] << plausibles
          end


??

This post has been edited by NotarySojac: 22 June 2011 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,308
  • Joined: 21-June 11

Re: Loosing track of my var types in arrays (ruby debugging tips)

Posted 22 June 2011 - 12:07 PM

To create an array containing n empty arrays you can use Array.new like this:

my_array = Array.new(n) { [] }



Or if you know the number of subarrays at write-time and it is small enough you could just use an array literal like this:

my_array_with_three_empty_subarrays = [ [], [], [] ]



To print arrays (or anything else really) for debugging, you should use p, not puts. It will print the contents of the array in a way that makes their type explicit (as well as any meta-characters in strings etc.). Example:

puts [1, "2", "3\b4"]
# Prints:
# 1
# 2
# 4

puts 5
# Prints:
# 5

puts [5]
# Also prints:
# 5

p [1, "2", "3\b4"]
# Prints
# [1, "2", "3\b4"]

p 5
# Prints:
# 5

p [5]
# Prints:
# [5]



This way you can see exactly what types of objects your array contains.

Quote

empty arrays count as nil, and throw the error... hmm...


No, they don't. Empty arrays count as arrays. If you get an error about an object being nil, it definitely is nil and not an empty array.

You should step through your code with a real debugger and output the contents of @regions[sq_row(y,x)].plausible_slots[sq_col(y,x)] when the exception happens. My guess is that it's an empty array, so calling [0] on that returns nil and calling delete on that causes the NoMethodError.
Was This Post Helpful? 1
  • +
  • -

#4 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Loosing track of my var types in arrays (ruby debugging tips)

Posted 22 June 2011 - 12:29 PM

Thanks for the tips, this makes ruby so much easier to work with!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1