Early programming exercises on a "learning to program in Xxxx" course involve asking the user to enter some pieces of data (usually numbers), doing a calculation using those numbers, and printing out a result. For that's a quite common requirement for a simple program - and this is the first realistic application that many delegates will have written.
When you read a number in from the user, you have the user enter a series of characters and that's actually got to be translated as a number. If the user enters letters rather than digits, you need to be able to deal with the situation, and you'll often need to look at the number and decide if it's within the valid range.
As part of the
Learning to ptogram in Ruby course that ran last week, I wrote a new example of asking the user to enter a number, and then went through a series of examples each of which improved on the robustness of the program.
The first example - full source code
[here] - does no checking what so ever:
print "please enter a number from 3 to 7: "
yousaid = gets.to_i
puts "patronising congratulation #{yousaid}"
The second example - full source code
[here] - checks. And we then have to consider what to do if the check fails ... so there's a loop in the code, an error message to be generated if a mistake is made. Here the replacement for the line that reads the data:
yousaid = nil
loop do
yousaid = gets.to_i
break if yousaid >=3 and yousaid <= 7
print "error message! (please make this helpful) - try again "
end
Programs will usually read multiple values, and if they're to do so, you won't want to repeat this block of lines multiple times in such a situation, so it's a good idea to put all those lines into a named block (called a function or a method) which you can repeatedly call up with just a single statement. The complete program showing how that work is
[here]. The function code might look like this:
def getnumber(min,max)
yousaid = nil
loop do
yousaid = gets.to_i
break if yousaid >=min and yousaid <= max
print "error message! (please make this helpful) - try again "
end
return yousaid
end
and the single line to call that:
valgiven = getnumber(3,7)
In the program examples I've shown you so far, a user who enter a string of letters isn't properly catered for. There's nothing that says "has he really entered a number". In Ruby,
to_i returns 0 if there's no number given, which will be a problem if zero were to be a valid entry. By changing to the
Integer method for my final example (full source code
[here]), I have trapped the error - doing so using an
excpetion which is a built in trap that picks up the problem and runs my piece of rescue code. The loop becomes:
loop do
begin
yousaid = Integer gets
rescue
yousaid = nil
end
break if yousaid != nil and yousaid >=min and yousaid <= max
print "error message! (please make this helpful) - try again "
end
As the course proceeds through
Ruby Programming, I'll show delegates how to separate out common code such as our data entry function / method into a separate file, so that it can be shared not only between multiple requirements in the same program, but between multiple programs too. That's good, efficient coding .. and it also provides a consistent user interface for the user. A truely professional approach.
(written 2013-02-17)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R113 - Ruby - Further Input and Output [4502] Reading and parsing a JSON object in Ruby - (2015-06-01)
[4553] RUby - loading, using, changing, storing JSON format data - (2015-10-23)
[4676] Running shell (operating system) commands from within Ruby - (2016-05-18)
[4678] Expect with Ruby - a training example to get you started - (2016-05-18)
R111 - Ruby - Exceptions. [1875] What are exceptions - Python based answer - (2008-11-08)
[2615] String to number conversion with error trapping in Ruby - (2010-02-01)
[2620] Direct access to object variable (attributes) in Ruby - (2010-02-02)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2622] Handling unusual and error conditions - exceptions - (2010-02-03)
[3177] Insurance against any errors - Volcanoes and Python - (2011-02-19)
[3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
[3433] Exceptions - a fail-safe way of trapping things that may go wrong - (2011-09-11)
[3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
[4675] Exceptions in Ruby - throwing, catching and using - (2016-05-17)
Q100 - Object Orientation and General technical topics - Learning to Progam [116] The next generation of programmer - (2004-11-13)
[1605] Learning and understanding scripting programming techniques - (2008-04-08)
[1963] Best source to learn Java (or Perl or PHP or Python) - (2008-12-28)
[1985] Learning to program as a part of your job - (2009-01-10)
[2001] I have not programmed before, and need to learn - (2009-01-19)
[2048] Learning to program in PHP, Python, Java or Lua ... - (2009-02-19)
[2092] Tracking difficult bugs, the programmer / customer relationship - (2009-03-20)
[2286] New to programming? It is natural (but needless) for you to be nervous - (2009-07-14)
[2294] Can you learn to program in 4 days? - (2009-07-16)
[2326] Learn a new programming language this summer. - (2009-08-06)
[2504] Learning to program in ... - (2009-11-15)
[2505] I almost put the bins out this morning - (2009-11-16)
[2898] Programming Standards from the start! - (2010-08-02)
[2973] Learning to program - where to start if you have never programmed before - (2010-09-28)
[3120] Learning to write good programs in C and C++ - separating out repeated code - (2011-01-04)
[3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[4318] Learning to Program - how we start to teach you at Well House Consultants - (2014-11-16)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
[4324] Learning to program - variables and constants - (2014-11-22)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4326] Learning to program - comments, documentation and test code - (2014-11-22)
[4337] Learning to program sample program - past its prime, but still useful - (2014-12-02)
[4575] Learning not just what a program does, but how to design it in the first place. - (2015-11-06)
Some other Articles
A course is not just for a year - its for a careerFrom SalfordReally Simple RailsClear, concise examples - Ruby classes and objects.Reading and checking user inputs - first lessons - RubyWhich database should I use? MySQL v SQLiteRuby / SQLite3 example program, showing JOIN v LEFT JOINNew guest ... becoming returning guestAnnual Accounts - a big job for a small businessWeb and console - same principle, same code - Ruby example