TicTac TOE Get_move

Can we create methods like this?

class Board
def initialize
@grid
end

def
row,col=pos
@grid[row][col]
end

end

so how do we go about calling this method on an instance?

board=Board.new
board[pos].nil?

Is this correct?

Yes, that’s mostly corrent, but if [] is going to take two arguments, row and col, then passing it one argument, pos, won’t work. If pos is an array you can call [] like this: board[*pos] using the splat operator to separate the array into row and col.

You also might want to set grid to be equal to something in initialize. As it is your initialize method doesn’t do anything.

What Iam not getting is how can we call board[pos]?
pos is given as an array.
Usually how we call is board.method_name .Isn’t it?
But i see that its called as board[pos]. How?

#[] is a special method. It has syntax sugar so instead of calling it like board.[](pos) you can call it like board[pos]. It’s just a special case, kind of like when you add an equal sign to the end of a method, where if you have a method #grid= you can call it like board.grid=(value) or you can use the syntax sugar version, board.grid = value

Oh thats why.Got it.
Thank you.