G
Gavin Kistner
--Apple-Mail-1-775425937
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed
For this week's Ruby Quiz, I would like to use Highline for user
input. I understand how to get it to validate input based on a
regexp, but I'd like to do more (after that is automatically handled,
if possible).
After I get Highline to ensure that a user's input matches a square
on the board (which can be done with the simple regexp) I'd like it
to further ensure that there is a piece on the board, and that piece
belongs to the player whose turn it is.
Is it possible to pass a custom post-validation block to to a
Highline::Question, which will use the return value of that block to
determine if the question should be asked again? (If not, consider
this a feature request.)
I suppose it's not much different to do:
piece = nil
while !piece
loc = ask( "Piece to move:" ){ |q|
q.responses[ :not_valid ] = "Please specify a board
location, such as a8 or c3"
q.validate = /[a-h][1-8]/i
}
piece = board[ loc ]
if piece && piece.color != board.turn_color
puts "You can't move the piece at #{loc}, because it is not
yours!"
piece = nil
end
end
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed
For this week's Ruby Quiz, I would like to use Highline for user
input. I understand how to get it to validate input based on a
regexp, but I'd like to do more (after that is automatically handled,
if possible).
After I get Highline to ensure that a user's input matches a square
on the board (which can be done with the simple regexp) I'd like it
to further ensure that there is a piece on the board, and that piece
belongs to the player whose turn it is.
Is it possible to pass a custom post-validation block to to a
Highline::Question, which will use the return value of that block to
determine if the question should be asked again? (If not, consider
this a feature request.)
I suppose it's not much different to do:
piece = nil
while !piece
loc = ask( "Piece to move:" ){ |q|
q.responses[ :not_valid ] = "Please specify a board
location, such as a8 or c3"
q.validate = /[a-h][1-8]/i
}
piece = board[ loc ]
if piece && piece.color != board.turn_color
puts "You can't move the piece at #{loc}, because it is not
yours!"
piece = nil
end
end