Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Reading and checking user inputs - first lessons - Ruby

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 "

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 "
    return yousaid

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
      yousaid = Integer gets
      yousaid = nil
    break if yousaid != nil and yousaid >=min and yousaid <= max
    print "error message! (please make this helpful) - try again "

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)

Back to
Which database should I use? MySQL v SQLite
Previous and next
Horse's mouth home
Forward to
Clear, concise examples - Ruby classes and objects.
Some other Articles
A course is not just for a year - its for a career
From Salford
Really Simple Rails
Clear, concise examples - Ruby classes and objects.
Reading and checking user inputs - first lessons - Ruby
Which database should I use? MySQL v SQLite
Ruby / SQLite3 example program, showing JOIN v LEFT JOIN
New guest ... becoming returning guest
Annual Accounts - a big job for a small business
Web and console - same principle, same code - Ruby example
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4008_Rea ... -Ruby.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb