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))
Formatting your output - options available in Ruby

Almost every program I've ever written (and almost every program you'll ever write) is required to output a mixture of constant character strings and results - formatting the output into a human readable form, or adding in separators between values in data files which the next program in a chain will read. So it's no surprise that there are lots of ways of doing the job ... here are some examples from Ruby.

1. Sending each of the expressions (be it a constant, a variable, or an expression) to the language's print function as a list of elements for the print statement to join up:

print("Henry is ",henry," and Jenny is ",jenny," and the difference is ",$diff,"\n")

2. Embedding the variables within the language's string construct using a syntax provided by the language for doing so:

puts("Henry is #{henry} and Jenny is #{jenny} and the difference is #{henry-jenny}")

3. Converting all of the values to be output into strings, then building up a complete string in an expression or variable which is outout:

puts("Henry is "+henry.to_s+" and Jenny is "+jenny.to_s+" and the difference is "+$diff.to_s)

4. Using a formatted printing function, with a single string to be output which includes placeholders, and additional parameters to fill in those placeholders:

printf("Henry is %d and Jenny is %d and the difference is %d\n",henry,jenny,$diff)

Of course, someone's going to ask "which one is best", and I'm going to say "it depends".

Option 1 - a whole list of things to output - is a very messy syntax with lots of "," to go wrong, and is hard to read. So it's not my favorite

Option 4 formats AND prints at the same time, and the computer science purists will tell you that a function should only do one thing at a time - so you'll be better to use an sprintf to do the formatting followed by a puts or print to do the output.

Option 3 is the way you'll find it done in languages such as Java - using an overloaded + operator to join strings, and a conversion method to convert numeric values into strings prior to joining them. It tends to be a bit verbose, but it's thorough - so longer to initially code and more straightforward to maintain.

Option 2 - where variables are embedded within a string - is the neatest. It's not available in all languages - in Ruby, you can see the syntax above. In Perl and PHP, where variables always start with a particular special character which the " operator understands (usually $, sometime @ in Perl), there's no need for the signatory #{..to..} as it's implicit in the variable name:

print("Henry is $henry and Jenny is $jenny and the difference is $diff\n");

There's a full example showing the options above [here]. And the sample output in each case looks like

Henry is 34 and Jenny is 21 and the difference is 13

(written 2010-09-29)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R106 - Input and Output in Ruby
  [1587] Some Ruby programming examples from our course - (2008-03-21)
  [1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
  [2290] Opening and reading files - the ruby fundamentals - (2009-07-16)
  [2614] Neatly formatting results into a table - (2010-02-01)
  [2621] Ruby collections and strings - some new examples - (2010-02-03)
  [2893] Exclamation marks and question marks on ruby method names - (2010-07-28)
  [3429] Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09)
  [4499] Significant work - beyond helloworld in Ruby - (2015-05-27)
  [4678] Expect with Ruby - a training example to get you started - (2016-05-18)


Back to
Learning to program - where to start if you have never programmed before
Previous and next
or
Horse's mouth home
Forward to
Why do I need brackets in Ruby ... or Perl, Python, C or Java
Some other Articles
Christmas 2010 - Well House Manor, Melksham, Hotel
What is a factory method and why use one? - Example in Ruby
Creating, extending, traversing and combining Ruby arrays
Why do I need brackets in Ruby ... or Perl, Python, C or Java
Formatting your output - options available in Ruby
Learning to program - where to start if you have never programmed before
Some more advanced Perl examples from a recent course
Should the public sector compete with businesses? and other deep questions
Perl - doing several things at the same time
What does blessing a variable in Perl mean?
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/2974_For ... -Ruby.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb