Ruby: Why Are All Coordinates Getting Updated in an Array of Arrays

Ruby: Why are all coordinates getting updated in an array of arrays?

The problem is that you are placing the same instance in the row multiple times. To make each item in the array a new object change your array definition to the following:

arr = Array.new(5){ Array.new(5) {Case.new}   }

See this question for more info: How to create array of objects?

initialize array of arrays in ruby

Use the Array.new block form:

ar = Array.new(2) { [0, -1] }

Since the array literal [0, -1] is scoped to the block, a new instance is created on every invocation. You can test this by adding a puts or similar in the block:

ar = Array.new(2) { |n| puts "called for element #{n}"; [0, -1] }

results in:

called for element 0
called for element 1
=> [[0, -1], [0, -1]]

create an array of objects with coordinates in Ruby?

@places = (0...width).to_a.product((0...height).to_a).map do |x, y|
Place.new(x,y)
end

Thanks @tadman, there are to be triple-dotted ranges to exclude the tight borders from ranges ([0, width)). That makes an amount of elements exactly equal width and height.

Unexpected result determining coordinates on a nested (grid) array in rails

You're not clearing the edit variable before each run of the outer loop. So each time you print it, it's still storing the initial coordinate where you found a 1.

You'll get the expected result if you insert edit = [] after puts edit.inspect.

This does what you want:

def output_image
x_size = @image.first.length
y_size = @image.length
edit = []
@image.each_with_index do | row , y |
row.each_with_index do |cell, x |
edit << [x,y] if cell == 1
end
end
edit.each do |pair|
y = pair.first
x = pair.last
@image[x-1][y] = 1 if x > 0
@image[x+1][y] = 1 if x < (x_size - 1)
@image[x][y-1] = 1 if y > 0
@image[x][y+1] = 1 if y < (y_size - 1)
end
@image.each { |x| puts x.join }
puts edit.inspect
end

Evaluating values and returning coordinates in a 2d array

array.each_index do |x|

captures this index into x variable and then you are trying to use each_index on number. Namely

x.each_index do |y|

Fixing this issue gives us the function:

def findMatchingCords(array)
array.each_index do |x|
array[x].each_index do |y|
if array[x][y] == 1 then
puts "X: #{x} Y: #{y}"
end
end
end
end

Now findMatchingCords(array) yields

X: 0 Y: 0
X: 1 Y: 1
X: 2 Y: 0
X: 2 Y: 3
X: 3 Y: 1

as expected.

Checking for (x,y) in two 1D arrays one x and one y in Ruby

You should write your checker like this one:

coordinates = loop do
randomX = 2 + rand(18)
randomY = 2 + rand(18)
randomNumberIsSnake = false
$x.size.times do |index|
randomNumberIsSnake = true if $x[index] == randomX && $y[index] == randomY
end
break [randomX, randomY] unless randomNumberIsSnake
end

Then in your coordinates local variable you'll have this values.

But I'd recommend you to store all free cell for current moment. Then take one randomly and remove from this array. If for some step array is empty there is no free cells at all. This solution is possible if that's not huge amount of cells.

Splitting an array using each_with_slice but keeping specific value(s) in resultant arrays (one liner)

This is the first option I found:

full_line_coords.flat_map { |e| e.each_slice(2).each_cons(2).to_a } 

Find the methods in Enumerable class and Array class



Related Topics



Leave a reply



Submit