[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[Highline] Custom validation

Gavin Kistner

6/13/2005 1:25:00 PM

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


--
(-, /\ \/ / /\/

2 Answers

Gavin Kistner

6/13/2005 1:30:00 PM

0

On Jun 13, 2005, at 7:24 AM, Gavin Kistner wrote:
> 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.)

The answer is "RTFM past the first sentence describing the
Question#validate property, onto the second sentence."

Sorry for the noise :p

James Gray

6/13/2005 5:04:00 PM

0

On Jun 13, 2005, at 8:30 AM, Gavin Kistner wrote:

> On Jun 13, 2005, at 7:24 AM, Gavin Kistner wrote:
>
>> 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.)
>>
>
> The answer is "RTFM past the first sentence describing the
> Question#validate property, onto the second sentence."
>
> Sorry for the noise :p

Looks like you found your answer, but for the sake of completeness
HighLine validation can be done with a Proc:

ask("Piece to move: ") { |q| q.validate = lambda { |move| is_legal?
move } }

James Edward Gray II