Ruby Code Examples

This page illustrates the Ruby Code Examples, in a straight way than to lengthy explanations, this can be used like an immediate reference for both syntax and programming ideas. Concise methods attempting a problem with a pragmatic approach are also discussed.

Strings
String interpolation allows a variable to get replaced by its value while execution %Q and %q are string literals that are with special properties Multiline Strings are defined with '<<' prefix to a named delimiter, like Ruby is really flexible with strings The string delimiter pairs are '', "", but with %, %q and %Q prefix, we have //, {}, ##, as delimiters, the unofficial rule is to use the delimiter that is not used to declare the string itself, this is to declare raw strings to avoid delimiter collision

Objects
An Object is something that can be handled with defined Methods, even for the physical objects arround us there are defined methods in using them, whenever we say 'object' it is to mean that it has some methods through which the object can be handled Ruby is object oriented, everything is an object including numbers, strings and even the nil

Enumerators, Ranges, Arrays and Hashes
Each of these can use the each method for iteration, though they seems to be synonyms, there is lot of difference in computing, The Enumerators and Ranges are almost of fixed memory length and so are memory efficient and fast, whereas Arrays and Hashes involves complex data structures for to be dynamic

Arrays
An array is a collection of elements, by default an array of n elements has its index enumerated from 0 to n-1, i.e, the index to the first element of an array is 0 array of arrays, array of ranges and arrays of mixed datatypes are allowed to declare, Object Oriented!

Hashes
Hash is an associative array which is a collections of Keys and it's associated Values