Need help with my code

having problems with def method

Page 1 of 1

4 Replies - 1584 Views - Last Post: 15 June 2010 - 01:41 PM Rate Topic: -----

#1 Coolwave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-March 10

Need help with my code

Posted 21 April 2010 - 11:25 AM

battleship=Array.new(9){Array.new(9)}
#def to allow players to be able to call for the board
def board
battleship[0][1]="A"
battleship[0][2]="B"
battleship[0][3]="C"
battleship[0][4]="D"
battleship[0][5]="E"
battleship[0][6]="F"
battleship[0][7]="G"
battleship[0][8]="H"
battleship[0][0]=" "
battleship[1][0]="1"
battleship[2][0]="2"
battleship[3][0]="3"
battleship[4][0]="4"
battleship[5][0]="5"
battleship[6][0]="6"
battleship[7][0]="7"
battleship[8][0]="8"
unknown="~"
for x in (1..8)
  for y in (1..8)
    battleship[x][y]=unknown
    end
  end
battleship.each do |myval|
  puts myval.to_s
  end
end
puts board



I'am new to programming and to ruby code, i'm having a hard time using the def method to use to call it up and display it. I get this error code:
C:/Users/Unknown/AppData/Local/Temp/rbB39C.tmp:4:in `board': undefined local variable or method `battleship' for main:Object (NameError)
from C:/Users/Coolwave/AppData/Local/Temp/rbB39C.tmp:31:in `<main>'

Any help would be great!! thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with my code

#2 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Need help with my code

Posted 21 April 2010 - 11:34 AM

Please, stop abusing Ruby. Do not try to write Java or C++ code in Ruby.
Get some books and read.

This post has been edited by MitkOK: 21 April 2010 - 11:34 AM

Was This Post Helpful? -1
  • +
  • -

#3 Coolwave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-March 10

Re: Need help with my code

Posted 21 April 2010 - 12:07 PM

View PostMitkOK, on 21 April 2010 - 10:34 AM, said:

Please, stop abusing Ruby. Do not try to write Java or C++ code in Ruby.
Get some books and read.


Not sure what you mean what part is not ruby? i guess that's what i'm having problems with. I don't know Java or C++.
i'm in a course and i have this project that needs to be done. Any help would be great thanks. P.S i didn't know i was abusing ruby sorry.
Was This Post Helpful? 0
  • +
  • -

#4 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Need help with my code

Posted 21 April 2010 - 12:22 PM

You should move array declaration inside board method.
Also you can use only battleship=Array.new(9){[]} to define array.
Was This Post Helpful? 0
  • +
  • -

#5 sublimeike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-October 09

Re: Need help with my code

Posted 15 June 2010 - 01:41 PM

View PostMitkOK, on 21 April 2010 - 10:34 AM, said:

Please, stop abusing Ruby. Do not try to write Java or C++ code in Ruby.
Get some books and read.



Wait to be productive and helpful....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1