James Gray
4/22/2005 3:32:00 PM
On Apr 22, 2005, at 10:12 AM, Christian Neukirchen wrote:
> Ruby Quiz <james@grayproductions.net> writes:
>
>> What I really think we need here is to take a page out of the
>> optparse book.
>> Here are some general ideas:
>>
>> age = ask("What is your age?", Integer, :within => 0..105)
>> num = eval "0b#{ ask( 'Enter a binary number.',
>> String, :validate => /^[01_]+$/ ) }"
>
> Do not ever do that.
>
> irb(main):004:0> Integer("0b1011")
> => 11
> irb(main):005:0> "1011".to_i(2)
> => 11
>
> irb(main):006:0> "foo\nbar" =~ /^foo$/
> => 0
I'm not exactly sure what you're trying to show here. Even 0 would be
a fine binary number. However, the point was that I have created a
safe eval() because ask() should not return anything that doesn't
validate.
Obviously, using Integer() is better style though.
James Edward Gray II