[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[QUIZ] HighLine (#29

James Gray

4/22/2005 12:46:00 PM

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rub...

3. Enjoy!

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

When you stop and think about it, methods like gets(), while handy, are still
pretty low level. In running Ruby Quiz I'm always seeing solutions with helper
methods similar to this:

# by Markus Koenig

def ask(prompt)
loop do
print prompt, ' '
$stdout.flush
s = gets
exit if s == nil
s.chomp!
if s == 'y' or s == 'yes'
return true
elsif s == 'n' or s == 'no'
return false
else
$stderr.puts "Please answer yes or no."
end
end
end

Surely we can make something like that better! We don't always need Rails or a
GUI framework and there's no reason writing a command-line application can't be
equally smooth.

This week's Ruby Quiz is to start a module called HighLine (for high-level,
line-oriented interface). Ideally this module would eventually cover many
aspects of terminal interaction, but for this quiz we'll just focus on getting
input.

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_]+$/ ) }"

if ask_if("Would you like to continue?") # ...

None of these ideas are etched in stone. Feel free to call your input method
prompt() or use a set of classes. Rework the interface any way you like. Just
be sure to tell us how to use your system.

The goal is to provide an easy-to-use, yet robust method of requesting input.
It should free the programmer of common concerns like calls to chomp() and
ensuring valid input.


9 Answers

Francis Hwang

4/22/2005 2:24:00 PM

0

Hi,

Not that this necessarily mitigates the educational value of this Quiz,
but: EasyPrompt sort of does what you're saying below.

http://easyprompt.ruby...

Example:

irb(main):001:0> require 'easyprompt'
=> true
irb(main):002:0> prompt = EasyPrompt.new
=> #<EasyPrompt:0x5a42a0
@stdout=#<EasyPrompt::MockableStdout:0x5a3e04>>
irb(main):003:0> fname = prompt.ask( "What's your first name?" )
What's your first name? John
=> "John"
irb(main):004:0> lname = prompt.ask( "What's your last name?", "Doe" )
What's your last name? [Doe]
=> "Doe"
irb(main):005:0> correct = prompt.ask( "Is your name #{ fname } #{
lname }?", true, :boolean )
Is your name John Doe? [y]
=> true

It's mockable, too! Everything must be mockable.



On Apr 22, 2005, at 8:45 AM, Ruby Quiz wrote:

> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this
> quiz until
> 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rub...
>
> 3. Enjoy!
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> =-=-=-=-=-=
>
> When you stop and think about it, methods like gets(), while handy,
> are still
> pretty low level. In running Ruby Quiz I'm always seeing solutions
> with helper
> methods similar to this:
>
> # by Markus Koenig
>
> def ask(prompt)
> loop do
> print prompt, ' '
> $stdout.flush
> s = gets
> exit if s == nil
> s.chomp!
> if s == 'y' or s == 'yes'
> return true
> elsif s == 'n' or s == 'no'
> return false
> else
> $stderr.puts "Please answer yes or no."
> end
> end
> end
>
> Surely we can make something like that better! We don't always need
> Rails or a
> GUI framework and there's no reason writing a command-line application
> can't be
> equally smooth.
>
> This week's Ruby Quiz is to start a module called HighLine (for
> high-level,
> line-oriented interface). Ideally this module would eventually cover
> many
> aspects of terminal interaction, but for this quiz we'll just focus on
> getting
> input.
>
> 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_]+$/ ) }"
>
> if ask_if("Would you like to continue?") # ...
>
> None of these ideas are etched in stone. Feel free to call your input
> method
> prompt() or use a set of classes. Rework the interface any way you
> like. Just
> be sure to tell us how to use your system.
>
> The goal is to provide an easy-to-use, yet robust method of requesting
> input.
> It should free the programmer of common concerns like calls to chomp()
> and
> ensuring valid input.
>
>

Francis Hwang
http://f...



James Gray

4/22/2005 2:38:00 PM

0

On Apr 22, 2005, at 9:24 AM, Francis Hwang wrote:

> Hi,
>
> Not that this necessarily mitigates the educational value of this
> Quiz, but: EasyPrompt sort of does what you're saying below.
>
> http://easyprompt.ruby...

Thanks for the link. I wasn't aware of this project. I like it.

James Edward Gray II



Bill Atkins

4/22/2005 2:47:00 PM

0

Is using a class instead of a module allowed?

On 4/22/05, Ruby Quiz <james@grayproductions.net> wrote:
>
> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this quiz
> until
> 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rub...
>
> 3. Enjoy!
>
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>
> When you stop and think about it, methods like gets(), while handy, are
> still
> pretty low level. In running Ruby Quiz I'm always seeing solutions with
> helper
> methods similar to this:
>
> # by Markus Koenig
>
> def ask(prompt)
> loop do
> print prompt, ' '
> $stdout.flush
> s = gets
> exit if s == nil
> s.chomp!
> if s == 'y' or s == 'yes'
> return true
> elsif s == 'n' or s == 'no'
> return false
> else
> $stderr.puts "Please answer yes or no."
> end
> end
> end
>
> Surely we can make something like that better! We don't always need Rails
> or a
> GUI framework and there's no reason writing a command-line application
> can't be
> equally smooth.
>
> This week's Ruby Quiz is to start a module called HighLine (for
> high-level,
> line-oriented interface). Ideally this module would eventually cover many
> aspects of terminal interaction, but for this quiz we'll just focus on
> getting
> input.
>
> 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_]+$/ ) }"
>
> if ask_if("Would you like to continue?") # ...
>
> None of these ideas are etched in stone. Feel free to call your input
> method
> prompt() or use a set of classes. Rework the interface any way you like.
> Just
> be sure to tell us how to use your system.
>
> The goal is to provide an easy-to-use, yet robust method of requesting
> input.
> It should free the programmer of common concerns like calls to chomp() and
> ensuring valid input.
>
>


--
Bill Atkins

Bill Atkins

4/22/2005 2:57:00 PM

0

Disregard. :)

On 4/22/05, Bill Atkins <batkins57@gmail.com> wrote:
>
> Is using a class instead of a module allowed?
>
> On 4/22/05, Ruby Quiz <james@grayproductions.net> wrote:
> >
> > The three rules of Ruby Quiz:
> >
> > 1. Please do not post any solutions or spoiler discussion for this quiz
> > until
> > 48 hours have passed from the time on this message.
> >
> > 2. Support Ruby Quiz by submitting ideas as often as you can:
> >
> > http://www.rub...
> >
> > 3. Enjoy!
> >
> >
> > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> >
> > When you stop and think about it, methods like gets(), while handy, are
> > still
> > pretty low level. In running Ruby Quiz I'm always seeing solutions with
> > helper
> > methods similar to this:
> >
> > # by Markus Koenig
> >
> > def ask(prompt)
> > loop do
> > print prompt, ' '
> > $stdout.flush
> > s = gets
> > exit if s == nil
> > s.chomp!
> > if s == 'y' or s == 'yes'
> > return true
> > elsif s == 'n' or s == 'no'
> > return false
> > else
> > $stderr.puts "Please answer yes or no."
> > end
> > end
> > end
> >
> > Surely we can make something like that better! We don't always need
> > Rails or a
> > GUI framework and there's no reason writing a command-line application
> > can't be
> > equally smooth.
> >
> > This week's Ruby Quiz is to start a module called HighLine (for
> > high-level,
> > line-oriented interface). Ideally this module would eventually cover
> > many
> > aspects of terminal interaction, but for this quiz we'll just focus on
> > getting
> > input.
> >
> > 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_]+$/ ) }"
> >
> > if ask_if("Would you like to continue?") # ...
> >
> > None of these ideas are etched in stone. Feel free to call your input
> > method
> > prompt() or use a set of classes. Rework the interface any way you like
> > Just
> > be sure to tell us how to use your system.
> >
> > The goal is to provide an easy-to-use, yet robust method of requesting
> > input.
> > It should free the programmer of common concerns like calls to chomp()
> > and
> > ensuring valid input.
> >
> >
>
>
> --
> Bill Atkins




--
Bill Atkins

James Gray

4/22/2005 3:07:00 PM

0

On Apr 22, 2005, at 9:46 AM, Bill Atkins wrote:

> Is using a class instead of a module allowed?

Definitely! Rework the interface any way you like.

James Edward Gray II



Christian Neukirchen

4/22/2005 3:12:00 PM

0

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

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


James Gray

4/22/2005 3:32:00 PM

0

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



Bill Kelly

4/22/2005 3:42:00 PM

0

From: "James Edward Gray II" <james@grayproductions.net>
>
> >> 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.

I think he meant along the lines of:

irb --simple-prompt
>> p "looks ok!" if "01010\nsystem('rm -rf /')" =~ /^[01_]+$/
"looks ok!"


Regards,

Bill




James Gray

4/22/2005 3:48:00 PM

0

On Apr 22, 2005, at 10:41 AM, Bill Kelly wrote:

> I think he meant along the lines of:
>
> irb --simple-prompt
>>> p "looks ok!" if "01010\nsystem('rm -rf /')" =~ /^[01_]+$/
> "looks ok!"

Oops, my Perl habits are showing. My bad. The Regexp should be
/\A[01_]+\Z/. I'll correct it on the Ruby Quiz site.

Thanks.

James Edward Gray II