Getting started with Ruby – Part 5

Hi in my previous article Getting started with Ruby – Part 4 I covered Hashes & blocks. Today I will take you through the various forms of loops in Ruby.

  • While – executes code as long as the given condition is true. We can separate the code from the condition using either the keyword do, a newline or a semi-colon (;)
  • Until – is very similar to the while loop with respect to the syntax & usage, the only difference between the two is the fact that the while loop executes the code block as long as the condition is true, the until loop does exactly the opposite, i.e. it executes the code block as long as the condition is false.
  • For – executes a block of code once for each element in the expression / object passed to it. Again as in the case of while & until above, the block of code may be separated from the expression by either using the keyword do, a newline or a semi-colon (;)
  • These are some of the other loops which are available through fixnums
  • Collections provide various different & interesting forms of looping mechanisms.

Loop flow control modifiers:

  • Break – terminates the immediate loop currently executing
  • Next – terminates the current iteration and beings the next.
  • Redo – restarts the current iteration.

That having done, let me now get started with the other basic

Variables

Any plain lowercase word is qualified to be an identifier or a variable name. Variable names may contain letters, underscores or digits except that a variable name cannot begin with a digit.

Symbols

Symbols are very similar to variables, except they start with a colon (:), they too may contain letters, underscores or digits except that they can’t begin with a digit (i.e. after the colon). Symbols are lightweight strings, they take up less memory, are faster and are immutable. These are generally used where you don’t need to print them out and also are rightly used for keys in hashes.

Constants

Constants in Ruby are words again very much like variables, except that they are capitalized. Constants as the name suggests are used for storing constant values which will not change over time. The most prominent usage of constants comes in defining classes, whose names are always constants, we will see classes later in much more detail. If you try to reassign an already assigned constant, the ruby interpreter will throw a warning and let you know, however the value will still change

Methods

Classes from a generic OOPs concepts is nothing but an encapsulation of the properties & functions to work on those properties. Methods are those functions. Methods in ruby are referred to as messages that are send to the caller object, methods may optionally take arguments as input.

In Ruby all methods return something, by default the last statement within the method’s definition determines what is returned. Although we have the return keyword in ruby, we do not always need to use it since we know the fact that the last line of the method body is always returned, hence when returning a value we can simply just have that value as the last line of the method definition and it will be returned.

Global Variables

Variables that begin with a dollar ($) sign are global. They are accessible from anywhere within your program, they never go out of scope, since they are in global scope.

That covers a lot for one post :). For now I think this much is enough for us to get started with coding something more concrete than what we have up until now. So in my next article I will take up a smallish problem to work on and we’ll see the solution for it. After that we’ll move onto classes & modules. Hope these articles benefit more people than I think. If you have any queries, suggestions or feedback, please let me know in the comments below.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Leave a Reply