Main Content
Learning to program in Ruby - examples of the programming basics Archive - Originally posted on "The Horse's Mouth" - 2009-07-15 07:40:33 - Graham Ellis
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.
Some other articles
R104 - Control Structures Alternating valuses / flip-flop / toggle - example in Ruby Where does Ruby load modules from, and how to load from current directory Separating your code for easier testing, understanding and re-use; example in Ruby Finding sum, minimum, maximum and average in Python (and Ruby) Conditionals, loops and methods in Ruby - a primer with simple examples Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Muttable v immutable and implications - Ruby Finding the total, average, minimum and maximum in a program Ruby v Perl - a comparison example Assigning values to variables within other statements - Ruby Does a for loop evaluate its end condition once, or on every iteration? Multiple inputs, multiple out, ruby functions Is this number between? Does this list include? - Ruby How a for loop works Java, Perl and other languages Returning multiple values from a function call in various languages - a comparison Ruby training - some fresh examples for string handling applications Splitting data reading code from data processing code - Ruby Why do I need brackets in Ruby ... or Perl, Python, C or Java Alternative loops and conditionals in Ruby and Perl For loop - checked once, or evety time? Ruby v Perl comparison and contrast Passing code to procedures and yield in Ruby A short form of if ... then ... else This article Ruby, Perl, Linux, MySQL - some training notes Ruby to access web services Ruby Programming Course - Saturday and Sunday What to do with a huge crop of apples Clean code, jump free (Example in Lua) Saying NOT in Perl, PHP, Python, Lua ... Some Ruby programming examples from our course Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) A better alternative to cutting and pasting code Ruby's case - no break Equality in Ruby - == eql? and equal? Breaking a loop - Ruby and other languages 1st, 2nd, 3rd revisited in Ruby R103 - Basic Ruby Language Elements Where does Ruby load modules from, and how to load from current directory Ruby - the second rung of learning the language Learning to program - variables and constants BODMAS - the order a computer evaluates arithmetic expressions Ruby - standard operators are overloaded. Perl - they are not Sigils - the characters on the start of variable names in Perl, Ruby and Fortran Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. Comparing floating point numbers - a word of caution and a solution Constants in Ruby Variable scope - what is it, and how does it Ruby? This article puts - opposite of chomp in Ruby R102 - Hello Ruby Objects Separating detailed data code from the main application - Ruby example Thin application, thick objects - keep you main code simple. Example in Ruby Easy example - data record to object and object to data record mapping in Ruby Ruby - the second rung of learning the language Clear, concise examples - Ruby classes and objects. Ruby objects - a primer This article R101 - Introduction Ruby - where one statement ends and the next begins Ruby - the second rung of learning the language Answers on Ruby on Rails This article New to programming? It is natural (but needless) for you to be nervous Ruby Programming Course - Saturday and Sunday Python v Ruby Ruby, Ruby, Ruby. Rails, Rails, Rails. Learnt in London - Ruby, Martini, Coral and the Core Cue the music, I'm happy. Ruby course - oops - it's not happening 2006 - Making business a pleasure