1. Computing

Advanced Ruby

These pages deal with some of the more advanced concepts of Ruby and are geared toward intermediate and advanced programmers. The covered concepts will build upon the more basic features of Ruby, a discussion of which can be found in Beginning Ruby.
  1. Bundler (3)
  2. Convoluted Constructions (3)
  3. Ruby Version Manager (12)
  4. Serialization (2)
  5. Test-Driven Development (13)

Tainted Data
In large programs, especially when heavy abstraction is involved, it's sometimes impossible to tell where your data is coming from. Consider the following situation: a server application takes user input from a client and does some processing. It then offloads some of this processing (for example, resizing an image file) to an external program using the backtick operator. The command passed t…

Making Deep Copies in Ruby
It's often necessary to make a copy of a value in Ruby. While this may seem simple, and it is for simple objects, as soon as you have to make a copy of a data structure with multiple array or hashes on the same object, you will quickly find there are many pitfalls.

Optional Method Parameters with Hashes
Using hashes to simulate optional named parameters in methods.

Refinements: Monkeypatching Made Safe
Monkeypatching base classes in Ruby is a typical Rubyism. It's too expressive and flexible to ignore. But there's always a downside, and in this case you're redefining how base classes (such as String and Fixnum) work, and this can have unintended consequences. This is where refinements, a new feature in Ruby 2.0, comes in.

Long Form and Short Form Command-line Options
There are two forms of command-line options: long form and short form. Both long and short form options take arguments, each a little differently.

OptionParser--Parsing Command-line Options the Ruby Way
Ruby comes equipped with a powerful and flexible tool to parse command-line options, OptionParser. The class is an alternative to GetoptLong for analyzing command line options.

Using OptionParser to Parse Commands in Ruby
Using OptionParser to parse command line options is a preferable alternative to GetoptLong or looking through ARGV manually. But how do you use it?

Writing Domain Specific Languages
A short guide on writing Domain Specific Languages in Ruby.

Ruby Best Practices--Making Quick Exceptions
Knowing how to make quick exceptions to catch and tell the difference between several conditions is a Ruby best practice you can't afford not to know.

Ruby's Bignum Library
Computers are quite good at math or, more precisely, at arithmetic. In fact, technically, that's all a computer can do. However, the numbers computers typically work with are quite limited. On a typical computer, an integer number is represented by a 32-bit binary number. A 32-bit integer can only represent the numbers from about negative 2 billion to positive 2 billion.

RubyQuiz
Challenges to test your Ruby programming skills.

Debugging Ruby
Debugging Ruby programs using the Ruby debugger.

Running the Ruby Profiler
Running the Ruby profiler to determine which methods are most expensive and which need to be optimized.

Ruby @ PLEAC
Ruby recipes at PLEAC (Programming Language Examples Alike Cookbook), the example programs from the Perl Cookbook re-written in Ruby.

An Introduction to Distributed Ruby
Distributed Ruby, or DRb, is a library that allows you to communicate with remote Ruby objects and programs, using TCP/IP network protocols.

The ‘exec’ Method
There are several ways to run other programs in Ruby. There is the system> method, backticks, etc. However, they all spawn new processes. If there is nothing left for your Ruby program to do, it has to sit there wasting memory until the launched program finishes. The exec> method fixes this.

Eval: Running Code on the Fly
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code.

21 Ruby Tricks
A few tips and tricks to make things a little easier.

Bindings
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed?

How to Display Your Ruby Version in Your Bash Prompt
With RVM installed on your system, it's easy to have many versions of Ruby installed. And, especially if you switch between them often, it's easy to forget which Bash command prompt is using which Ruby version. You can solve this by putting your Ruby version in your Bash prompt.

Creating and Distributing Gems with Bundler
Creating Ruby gems has always been a bit of a pain. When you get right down to it, it's not a hard thing to do, but it's easy to forget how if you don't do it often enough. There are quite a few gems to help you write other gems, but if you're using Rails 3, there's already a gem on your system that does this: Bundler.

Ruby Core
Information on installing, running and contributing to the development of the latest release of Ruby.

Refinements: The End of Monkey Patching
Ruby is an extremely flexible language. It's a truly object oriented language, meaning everything is an object that responds to messages. It's also an open language, meaning you can not only add new methods to any class, but replace any method in any class. This has led to something that's at once great, and terrible: monkey patching.

Installing Gems from Git
How to install gems from git repositories.

Using the Enumerable Module
By using the Enumerable module, you can make any of your classes act like Ruby standard collections. All you need to do is implement the 'each' method.

Garbage Collection
The Ruby garbage collector is a mysterious thing, but it's not very complicated.

Calculating Abbreviations
Ruby provides an easy way to make unique abbreviations of a set of words.

Base64 in Ruby
Base64 is a way to represent any data as an ASCII string. It's used to encode files to send in email or other ASCII-only protocols. Ruby provides a few easy methods to encode and decode Base64 data.

Password Security Primer
When web developers fail to implement strong password security it's the users that suffer. This primer will teach you what you need to know in order to safely store passwords in a database.

Cryptographic Hashes in Ruby
Generating cryptographic hashes, such as MD5 or SHA1 hashes, in Ruby is very easy. Ruby provides two libraries for doing this: a builtin Digest library and access to the OpenSSL library.

References to Methods in Ruby
Taking a reference to a method doesn't sound like it should be possible. A method cannot exist on its own, right? Right, but Ruby does allow you to easily pair a reference to a method and the object it belongs to.

Weak References
Weak references store a reference to an object without incrementing the reference counter. This allows you to reference an object and still allow it to be collected by the garbage collector.

Why Does 'map' Return an Array?
Why does 'map' always return an Array? It doesn't matter what type of object you call it on, as long as it has a 'map' method, it will return an array. Why?

Convoluted Constructions: a = 1, b = 2
Given the expression a = 1, b = 2, what will a and b be equal to? What will their types be? Why?

Ruby Tricks: Expressive Case Statements

Ruby Tip Smorgasbord

Exploiting the Garbage Collector
Ruby gives you access to every single object in memory via the ObjectSpace module. While this is not very useful in most situations, there are a few times when you might want to use this module.

More That Static Regular Expressions
Regular expressions can do more than you think.

©2014 About.com. All rights reserved.