Articles Index
Challenge: Sequence of Integers
A challenge: Given an integer, write a method that takes an integer and must return an array containing all the integers from 1 to that integer.
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.
Throw and Catch
Throw and catch are somewhat related to exceptions, but also related to a language construct that's so hated it may be considered a curse word to some: goto.
Exceptions
Not everything goes right all of the time. When something does go wrong in Ruby, an exception will be thrown. It's up to you to catch them, but if you don't your program will exit.
String Substitution in Ruby
Splitting a string is only one way to manipulate string data. You can also make substitutions to replace one part of a string with another string. For instance, in an example string "foo,bar,baz", replacing "foo" with "boo" in "foo,bar,baz" would yield "boo,bar,baz". You can do this and many more things using the sub and gsub method in the String class.
Loops
Ruby has a the usual and expect loops, as well as a number of loops specific to Ruby. It even has a way to define you own types of loops.
The Case Statement
The case statement is a control structure that is usually quite limited in other programming langauges. However, it's quite powerful and flexible in Ruby.
The Case Equality Operator
The case equality operator is key to understanding how case statements really work, and is overall a useful operator when you want to make "fuzzy comparisons."
Conditional Statements
Conditional statements are what makes computer programs tick. Without conditional statements, all computer programs would follow the same path from beginning to end.
Boolean Operators
While simple boolean expressions will get you pretty far, you'll soon find yourself wanting to act on more than one condition. For that you'll need boolean operators.
Boolean Expressions
A "boolean" expression is any expression that is intended to be evaluatated as true or false. Any expression may be used as a boolean expression, but generally only expressions with comparison operators are used as boolean expression.
Everything You Need to Know About Variables
Variables are named references to objects in memory. Their several variants (local, instance, class, constant, etc) each have their own subtleties.
Class Variables
Class variables in Ruby are more or less as you'd expect form an object oriented language. However, Ruby throws you a curveball you can use to your advantage.
Global Variables
Global variables are not often used in Ruby, however, you should at least understand their usage should you ever encounter them.
Constant Variables
Ruby's idea of constant variables is mostly without surprise, however their implementation is not exactly "constant."
Local Variables and Bindings
Ruby has a no-surprise take on local variables, they're handled similarly in most programming languages. However, Ruby implements them differently than stack-based languages.
Instance Variables
Instance variables are some of the most common and important variables types in Ruby. They begin with the at sign (@) and allow objects to track their internal state.
CSV Example: Format Strings and Printing the ASCII Table
Now that all the data has been parsed from the CSV and sorted, it's time to print out the ASCII table. Format strings are used to print the data while staying in the lines, and a few tricks are used to make the ASCII table a bit more pretty.
CSV Example: Sorting and Column Widths
Once data has been parsed from CSV, the rows must be sorted and to prepare for formatting in an ASCII table, the column widths are calculated.
CSV Example: Parsing CSV
A crash course on what CSV is, what it is used for and how it is parsed. A method for parsing CSV files using only Ruby's built-in string methods is presented and discussed.
Splitting Strings
Unless user input is a single word or number, that input will need to be split, or turned into a list of strings or numbers.
Getting and Printing Strings
How to read and print strings in Ruby.
String Literals
The basics of strings, string interpolation, single and double quotes, escape sequences and alternate string literal syntaxes.
Indexing Strings
When strings are used for output, they're often viewed as single immutable blocks of text. Once you form the string using string literals and interpolation, it often is simply output or stored in a variable to be output later. However, when strings are used as input, they're not usually taken at face value.
The Mechanize 2.0 Handbook
Mechanize is a library for automated interaction with web sites. For all intents and purposes, it acts like a web browser with no user interface. It downloads web pages, can click on links, fill out and submit forms, store cookies, etc. Mechanize is useful for automated crawling, testing and scraping of web sites.
