We so often overlook the basics of programming, and yet they are so fundamental to good code - understanding things like how widely variables can be seen (also known as variable scope), what happens when you divide two numbers (do you get a decimal result or is the remainder thrown away), and how do you name a block of code and re-use it to avoid having to duplicate code and later maintainance work.
On yesterday's Ruby course, I produced a series of example of many of the basics and I have uploaded them on to our web site for you to take a look through if you wish. I have tried to avoid the temptation of "I'll just add something to show xxx" in these examples - the wood should not get lost in the trees!
[link] Prompt, read from user, calculate, output in Ruby
[link] Use of intermediate variables
[link] Use of intermediate variables - alternative
[link] Easier output formatting
[link] Calculations within double quotes
[link] if, elsif and else illustrated
[link] while, until and for loops - comparison
[link] finding extreme values from a collection
[link] Summing a collection
[link] Putting common code in a name block (function)
[link] Define class, create and use instances of it
[link] Use of 'self' in Ruby
[link] Calling program to access class in another file ...
[link] ... and that sample class in its own file
We offer two public Ruby courses -
Learning to program in Ruby for newcomers to programming, and
Ruby Programming for those with prior programming experience in another language. Public courses run at our
Well House Manor training centre in Melksham, Wiltshire, England. These are niche courses - we offer residential facilities to full hotel standard, and can help with delegates travel arrangements too.
(written 2009-07-15 07:40:33)
Associated topics are indexed under
R101 - Ruby - Introduction [2607] Answers on Ruby on Rails - (2010-01-30)
[2286] New to programming? It is natural (but needless) for you to be nervous - (2009-07-14)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[1375] Python v Ruby - (2007-10-02)
[1302] Ruby, Ruby, Ruby. Rails, Rails, Rails. - (2007-08-13)
[1041] Learnt in London - Ruby, Martini, Coral and the Core - (2007-01-17)
[1027] Cue the music, I'm happy. - (2007-01-09)
[664] Ruby course - oops - it's not happening - (2006-04-01)
[550] 2006 - Making business a pleasure - (2006-01-01)
R102 - Hello Ruby Objects [2603] Ruby objects - a primer - (2010-01-29)
R103 - Basic Ruby Language Elements [2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
[2613] Constants in Ruby - (2010-02-01)
[2296] Variable scope - what is it, and how does it Ruby? - (2009-07-18)
[986] puts - opposite of chomp in Ruby - (2006-12-15)
R104 - Ruby - Control Structures [2619] Passing code to procedures and yield in Ruby - (2010-02-02)
[2471] A short form of if ... then ... else - (2009-10-23)
[1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
[1891] Ruby to access web services - (2008-11-16)
[1870] What to do with a huge crop of apples - (2008-11-04)
[1738] Clean code, jump free (Example in Lua) - (2008-08-06)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1587] Some Ruby programming examples from our course - (2008-03-21)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[995] Ruby's case - no break - (2006-12-17)
[985] Equality in Ruby - == eql? and equal? - (2006-12-14)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[960] 1st, 2nd, 3rd revisited in Ruby - (2006-12-02)
Some other Articles
Collection objects (array and hash) in RubyOpening and reading files - the ruby fundamentalspre-Inaugural briefing - Melksham Community Area PartnershipWiltshire Community Area PartnershipsLearning to program in Ruby - examples of the programming basicsGreat new diagrams for our notes ... Python releasesStrings as collections in PythonEveryone is in the customer relations businessChecking robots.txt from Python