1

How would I write this java code into ruby:

String[] [] Score = new String [row] [col];
Score[rCount][cCount] = num;

I thought it would as simple as:

score=[]
score[rcount][ccount]=num

But I keep getting "undefined method `[]=' for nil:NilClass (NoMethodError)"

2
  • 1
    Please read stackoverflow.com/help/how-to-ask and the create a MCVE and then ask. People over here are trying to help you. The least you could do is format the question so that its more readable. Why do you need java tag ? Commented Nov 24, 2015 at 18:13
  • 1
    You could improve your question by removing the reference to Java. Just ask, for example, how you could create an array arr that permits arr[row][col] = 3 to set a value and arr[row][col] to return a value. Commented Nov 24, 2015 at 22:11

2 Answers 2

3

Sorry, I don't know java, but have a look at the class methods Array#new and Array::[], and the instance methods Array#[]= and Array#[]. Here are some examples that should answer your question (and other questions that may be sparked, hopefully):

Array.new #=> [] 
[]        #=> [] # shorthand for above

a = Array.new(5) { [] } #=> [[], [], [], [], []] 

a[0][0] = 2
a #=> [[2], [], [], [], []] 
a[3][2] = 4
a #=> [[2], [], [], [nil, nil, 4], []] 
a[1] << 1
a #=> [[2], [1], [], [nil, nil, 4], []] 
a[1] << 2
a #=> [[2], [1, 2], [], [nil, nil, 4], []] 
a[1] << 3 << 4
a #=> [[2], [1, 2, 3, 4], [], [nil, nil, 4], []] 
a[2] << [4,5]
a #=> [[2], [1, 2, 3, 4], [[4, 5]], [nil, nil, 4], []] 
a[4].concat([4,5]) 
a #=> [[2], [1, 2, 3, 4], [[4, 5]], [nil, nil, 4], [4, 5]] 

a = Array.new(3) { Array.new(3) }
  #=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]] 
a[1][2] = 4 
a #=> [[nil, nil, nil], [nil, nil, 4], [nil, nil, nil]] 

We could also write the default as a second argument:

a = Array.new(3,[]) #=> [[], [], []]

but that can be problematic:

a[0][0] = 'cat'
a #=> [["cat"], ["cat"], ["cat"]]

as is:

a = Array.new(3,Array.new(2)) #=> [[], [], []]
  #=> [[nil, nil], [nil, nil], [nil, nil]] 
a[0][0] = 'cat'
a #=> [["cat", nil], ["cat", nil], ["cat", nil]] 

since each element of a is the same array.

Note that Ruby provides a convenience for writing certain methods that is commonly referred to as "syntactic sugar". If, for example, you write a = [1,2,3], Ruby will interpret that as a = Array.[](1,2,3) (and you could write it that way), the class method being Array::[]. Similarly, if a equals [1,2,3], a[1] = 'cat' is decoded as a.[]=(1, 'cat') and a[1] #=> 'cat' is a.[](1). Similarly, h = {} translates to h = Hash.new and so on.

Note that Ruby does not have a concept of "multidimensional arrays". For more on that you may wish to see a comment a left on this question.

Sign up to request clarification or add additional context in comments.

Comments

1

Firstly, ruby programmers use snake case. Capital letter is using for class names.

Secondly, your problem happens just because

score[rcount] == nil # true

If you want to have an access to second dimension elements you need to initialize line as array:

score[rcount] = []

Now you can set second dimension element

score[rcount][ccount] = num

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.