Ruby's Basic Features: Blocks, Loops, Arrays & more
In any programming language, you need some way to prevent naming clashes. This is especially true when you're using at least one third party library, where you have no control over the naming scheme. In the past, naming conventions would be used. It's not uncommon to see C programs with names like mylibrary_some_function. However, Ruby has a more powerful solution: modules.
How Can I Store Key/Value Pairs in Ruby?
Storing key/value pairs in Ruby doesn't always need to be accomplished by using a hash. You can also use the Struct and OpenStruct classes.
Hashes are an integral part of Ruby. Where other languages may rely on simpler data structures, even Structs or objects, to store related values, Ruby relies on the Hash for this purpose.
How to Create and Use Hashes in Ruby
This article explains and teaches how to use a associative arrays (hashes) as a way to store multiple variables in one data structure.
Advanced Hash Usage
There's more to do with hashes than just store and retrieve objects. For example, you can use them to emulate keyword parameters.
Creating Hashes from Arrays
How do you build a Hash from Arrays? Without any research, you might resort to loops, but Ruby has some much more elegant ways of doing this.
These are the most used and most useful Hash methods. While there are more listed in the official documentation (which is linked to here, should you need it), these are the most useful.
Hash Tips and Tricks
There's more to Hashes than meets the eye. These tips and tricks will show you a thing or two about Hashes.
How to Create Arrays in Ruby
This article explains and teaches how to use an array and an associative array (hash) as a way to store multiple variables in one data structure.
Basic Array Operations
Beyond creating arrays from Array literals, there are a few things you should know to get by with Arrays: building Arrays at runtime using push and split, sorting arrays and using a few basic set operations on Arrays.
One of the most common tasks you'll need to perform when dealing with Arrays is to sort them. You've got numerical data and need to sort it for analysis, or you've got a list of names that need to be sorted for printing.
While it's quite useful already to just be able to create, iterate and manipulate arrays, there's one very useful array method you really shouldn't live without: map.
How to Use Loops in Ruby
How to use loops in Ruby
How to Use and Create Blocks in Ruby
How to use blocks in Ruby.
Input and Output
The basic IO methods are often overlooked by many Ruby programmers. How often have you used the gets and puts methods without giving them a second glance? Learn all the features of these methods to better put them to use in your code.
Ranges represent a range of objects, from a minimum to a maximum. Typically they're used to test if another object lies between the minimum and maximum, and to iterate over the range.
Load Vs. Require
You probably know all about the 'require' method. You've probably used it hundreds of times by now. And yes, it is a method, not a keyword. But it has a cousin you probably haven't used yet: load.
Introduction to Arrays
Arrays are Ruby's main way of handling ordered collections. This (and the Hash) is the most used data structure in Ruby.