4:50 pm Sunday May 20 2012
Use command-line arguments to pass parameters to your Ruby scripts from the command line.
This article explains and teaches how to use a associative arrays (hashes) as a way to store multiple variables in one data structure.
Learn how to use the command line interface on Windows, Linux and OS X to run rb files and use the interactive Ruby program.
Use comments to annotate your Ruby code.
The 'require' method is the reusable code mechanism in Ruby. It allows you to import other source files into your programs.
Installing the Ruby Version Manager (RVM) on Linux
Using environment variables in Ruby.
Splitting a string is only one way to manipulate string data. You can also make substitutions to replace
One of the most powerful, useful and often overlooked features of RVM is the gemset. A gemset is a separate gem directory, so the gems in one gemset don't conflict with the gems in another gemset.
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.
How to install Ruby on Apple OS X
This article explains and teaches how to use a arrays as a way to store multiple variables in one data structure.
String manipulation using the split method in Ruby. Split strings into a list of substrings.
Loops, including while and include loops, are one of the most commonly used constructs in computer programs.
How to install Ruby on the Linux operating system.
Aliasing is a powerful Ruby feature that allows more than one method to be referred to by multiple names. This can be used to give a programmer more expressive options or to create copies of a method, allowing you to change the behavior of a class.
How to install the Ruby interpreter on Windows, Linux and OS X.
What type of language is the Ruby programming language?
It's easy to jump into parsing and generating JSON in Ruby with the json gem. It provides an API for parsing JSON from text as well as generating JSON text from arbitrary Ruby objects. It's easily the most used JSON library in Ruby.
Problem: You encounter an error reading NameError: uninitialized constant Something or NameError: uninitialized constant Object::Something (with various class names in place of Something).
How to add files to a Git repository.
Using OptionParser to parse command line options is a preferable alternative to GetoptLong or looking through ARGV manually. But how do you use it?
How to install the Ruby interpreter on the Windows operating system.
In the previous article in this series, we discussed the basics of the Dir class. Particularly useful was the Dir.foreach method for iterating over all files in a directory. However, this only goes so far. What if you want to iterate over only some files (for example, just XML files) in a directory?
Installing command line tools such a Git on Windows is often difficult. Not only do you have to find a Windows port or attempt to compile the tool yourself from C source code, you also have to deal with the Windows command line. The Windows command line is, to say the least, a little lacking. Windows users are in luck here though, the MSysGit project solves both these problems.
Using and manipulating strings and substrings in Ruby.
How to use the logger library to create and write to log files 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.
Net::SSH is a way for Ruby to interact with SSH (Secure Shell) protocol. It relies on the OpenSSL library for encryption.
How to install gems from git repositories.
Now that you can write controller logic and views and Haml, you can start putting together some more complex application. There's one very annoying thing though: Sinatra does not reload its code whenever you make a change. If you make a change, you have to stop and restart the server. This is just an extra step and generally an annoyance. This where shotgun comes in.
There are multiple ways of installing Git on Linux. Depending on which Linux distribution you're using and whether it provides binary packages for Git, you can either install a binary package or install from source.
So, you've gotten your shiny new Mac and you want to get right into Ruby programming , where how do you
Debugging Ruby programs using the Ruby debugger.
The gem command is one of the most used Ruby-related commands. This article shows 25 Gem Commands and their functions.
Problem: You encounter an error reading NameError: undefined local variable or method `a' for #
There’s a lot of talk about Rack , but unless you’re a framework author yourself, you rarely see it. So what is Rack? And why, as an application developer, should you care about it?
In the previous article, you learned what Rack is. Now, it’s time to start using Rack and serve up some pages.
Sometimes the old adage rings true: the best things in life are free. If you've wanted to learn a little Ruby, but haven't been able to fit a programming course into your budget, don't worry. There are a number of places on the Web to begin your learning, without sacrificing money or quality.
Directions of how to install the Tk GUI and its Ruby bindings on Windows, Linux and Mac OS X.
How to use Haml to create Sinatra views and layouts.
How to use the command line interface on Windows, Linux and OS X to run Ruby scripts and use the interactive Ruby program.
How to get started in Sinatra, including how to use actions, parameters, ERB and views.
How to use blocks in Ruby.
Exceptions are used to handle things the unexpected things that might go wrong with your Ruby code. This article addresses what Exceptions are and how they can be used.
Ruby 1.9 introduces the concept of a named capture group. These named groups can be defined at the beginning of a regexp statement using a trick that tells for the regexp engine to look for exactly 0 instances of the group. Later, the group can be recalled using the \g element, followed by the name of the group. You can think of named capture groups as subroutines inside of a regular expression.
Learn to search the Ruby Gems repository to find gems to add and install to your library.
A feature in Rails that protects against Cross-Site Request Forgery (CSRF) attacks.
Using the Twitter RubyGem, making API requests to Twitter is even easier than using the twitter method.
For about 6 years now, there's been a lot of talk about this thing called "Ruby on Rails." They're saying
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.
RVM is great. However, there may be times where you must run Ruby scripts with RVM as root (or another user) via sudo.
The release of Ruby 1.9.1 brings new features to be learned,among them are the changes in block variable scope. Learn the new syntax for block variable scope introduced in Ruby 1.9.1.
Problem: You're trying to install a gem and you encounter an error reading no such file to load -- mkmf. This doesn't always occur, only when you're trying to install gems with native C code that must be compiled.
DataMapper is a "Object/Relational Mapper" or ORM. In other words, it maps Ruby objects to database records
Generating a Rails project from which to build an application.
What is cloud computing? Interviews with industry experts help us define cloud computing, software as service, platform as service and more.
Input and output methods are often overlooked by many Ruby programmers. How often have you used the <b>gets</b> and <b>puts</b> methods without giving them a second glance? Learn all the features of these methods to better put them to use in your code.
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.
Chances are, you've spent your entire database life with relational databases. Data gets organized into columns, and stored in rows. You manipulate data with SQL (Structured Query Language) and there's really no other way to do it. There are competing SQL servers out there (MySQL, SQLite, etc), but they all more or less operate in the same way. But there's much more out there than SQL databases, especially with all the new software in the past few years.
Can I add empty directories to Git repositories?
Aside from the basic regular expression operators such as groupings and quantifiers, there are some other special characters and escape sequences (backslash followed by a character) that can help you out when you're working with Ruby.
Running programs with Net::SSH is simple enough. The Session object gives you two convenient methods with which to accomplish this task.
Using lookahead and back-references to refer to other parts of the regular expression text.
So far, we've discussed how to use Sinatra in general, how to handle and route requests, and how to build views using Haml. Before we take the plunge into using data storage with Sinatra, let's put what we've discussed so far into practice. This article explains how to use Sinatra to provide a web interface for a gem.
Benchmarking measures the time it takes to execute code and compares it to other code that accomplishes the same task. This is useful to see which is faster, and gives you the opportunity to use the faster code in your program.
Serializing an object is to convert an object to a byte stream and either store that object for future loading, or sending to another program to be loaded there. There are several common ways to serialize objects in Ruby. The Marshal module is used less often than the other methods, but it's included in the standard library.
The Builder library is a Ruby Gem that uses a meta-language to generate HTML, XML or CSS markup.
Problem: Seemlingly at random, you've encountered the error undefined method `to_sym' for nil:NilClass, or a similar error message for any method name other than to_sym.
Using hashes to simulate optional named parameters in methods.
The Twitter API (Application Programming Interface) in Ruby on Rails is about as simple as you can get. Twitter is a RESTful Rails application.
RVM is the Ruby Version Manager. It allows you to install and manage several different versions and implementations of Ruby on one computer, including the ability to manage different sets of RubyGems one each.
A comparison of Ruby to other programming languages, including Perl, Java, Python and PHP.
Up until now, Hpricot HTML parser for Ruby has been the fast an easy standard. The Nokogiri Ruby gem can parse not only HTML, but also works with XML, CSS and XPath selectors.
If you're going to program in the Ruby language, there are some basic features you absolutely must know. Here are the Top 5.
YAML (YAML Ain't Markup Language) is perhaps the most common form of serialization in Ruby applications. It's used for configuration files in Rails and other projects, and is nearly ubiquitous. It also has distinct advantages over marshal, and is usually preferred.
Editing Rails projects in Vim effectively.
A visual tutorial with screenshots showing how to install the Rubygame library on Windows, including installing all needed DLL files.
What is cloud computing? We asked Dave Vandervort of Xerox Innovation Group to help us answer that question and more.
Learning how to work with files is one of the first things new Ruby programmers learn. However, how to work with directories of files is something that's often skipped or only lightly touched upon. Ruby does have some powerful mechanisms for working with directories though.
Running the Ruby profiler to determine which methods are most expensive and which need to be optimized.
Distributed Ruby, or DRb, is a library that allows you to communicate with remote Ruby objects and programs, using TCP/IP network protocols.
Since the Twitter API makes it easy to do anything you can do on the website, writing a Ruby script to automatically follow followers is very simple.
Using options to change the behavior of regular expressions.
Sinatra is a web framework. Simply put, it's a library that allows you to write web applications with a minimum of red tape.
The ternary (or "conditional") operator will evaluate an expression and return one value if it's true,
SSH is not just for running commands, it's a general conduit for encrypted communications between two hosts. One common use for SSH is to transfer files using the SCP protocol over SSH. You can use SCP in your Ruby programs with the Net::SCP library, which is a supplement to Net::SSH.
Learn to use Ruby's pre-installed POP3 libraries to check your email.
How do Ruby and Python compare in terms of features, form and function?
Mixins are one of Ruby's defining features, but often one of the most difficult to understand for those new to Ruby. They're not difficult to understand, but they're not something most programmers have encountered before as most languages don't have mixins.
We've all had to respond to that ubiquitous question: Are You Sure You Want to Exit? Ever wondered what was behind creating them? Shoes gives you 5 different dialog methods to choose from.
Among the new features in release of Ruby 1.9.1 is the Lambda operator, a shortcut used to make nameless methods. Learn how to use this feature introduced in Ruby 1.9.1.
Myth: Ruby is unsuitable for game development. It's much too slow, there are no good libraries and it's too hard to deploy your games written in Ruby.
Text adventure games were a very popular game genre on minicomputers and microcomputers from the 1970's through the 1990's. This article series will take you through each step of making a text adventure game in Ruby.
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.
Twitter provides programmers with an excellent API, which is why so many Twitter interfaces exist for tweeting, following updates, following friends and thread-sorting. Writing Twitter clients in Ruby is a breeze!
The syntax of a language is the grammatical rules a program must follow in order to be understood by the interpreter or compiler.
The grouping operators, including the parentheses operator, are some of the most powerful and frequently used operators in the Ruby regex syntax.
A package manager is software on a Linux system that manages the other software installed on the system.
Ruby makes it easy to run external programs and capture their output. Using the built-in "backtick" operator
Part four of the Ruby on Rails blog tutorial shows you how to create a comments table to allow users to comment on your blog posts.
Ruby on Rails 3 Tutorial: Learn Rails by Example by Michael Hartl is a hands on introduction to not only Ruby on Rails but all the related software and services many Rails developers use such as Git, Github, Heroku and RSpec. There are no long-winded discussions on theory or REST or MVC, each and every part of every chapter is a hands on exercise in building a Rails application. This book is recommended for anyone who prefers a hands on approach to learning.
What features do Ruby and the Java programming language share? How are they alike and how are they different?
The following is a list of Rack middleware applications that come with the Rack gem. A brief description of each middleware is given.
The basics of strings, string interpolation, single and double quotes, escape sequences and alternate string literal syntaxes.
How to interface with the Arduino's serial port on Linux.
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code.
How to send email with Ruby on Rails using SMTP.
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.
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.
A Git repository is not like a repository in most other version control systems. Normally, you'll have something like a CVS or Subversion server where the repository lives. From that, you'll check out a working copy, make your changes to that and commit them. Git is different though, there is no centralized server.
The Simple-RSS RubyGem provides a simplified interface for reading RSS and Atom documents.
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.
Like just about every programming language every conceived, Ruby supports constants. A constant is a variable whose value cannot be changed. However, in Ruby, this has some caveats.
How do do various types of URL matching in Sinatra.
Mechanize can be configured to use HTTP proxies. Some network configurations require you to use HTTP
In a Shoes application, the button is one of the simplest elements. This tutorial will teach you how to make the most of the your GUI toolkit in Ruby.
Learn the basics of writing and running unit tests for test driven development in Rails.
Why are cloud computing and Ruby on Rails a good match? Let's hear what Hampton Catlin, author of HAML and Wikipedia's head Ruby/mobile architect has to say!
This article will create a rolodex application that can be used to keep track of contact information. The total line count is about 130 lines, split about half and half with code and views.
Most likely, you'll be wanting to find links and click on them. There are three methods that help you
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.
Part three of the Ruby on Rails blog tutorial uses the RESTful authentication plugin to make sure only authorized users can make posts to your blog.
A review of the Vim text editor for writing Ruby code.
Using regular expression to tell if an email address is valid or not.
Much is made of the two base data structures in Ruby: the Array and the Hash. However, it's also quite easy to use two dimensional arrays in Ruby.
What are the similarities and dissimilarities between Ruby and PHP?
How to check POP3 email accounts in Ruby.
There are many hidden pitfalls when using temporary files. Ruby's Tempfile class solves many of these.
Instructions for installing Ruby on Windows, Mac OS X and Linux as well as instructions for installing a few Ruby-related extras.
Install Ruby 1.9.1 on your Linux machine alongside your current Ruby 1.8.x distribution to upgrade it. You'll have to compile the code from source, as shown in this tutorial.
If you already have a Ruby like 1.9.2-rc2 installed with a number of gems and want to upgrade to 1.9.2-p0, the following steps will get you there. As expected,RVM makes this quite easy.
Grouping your test cases into test suites.
Buttons primarily exist to submit forms. They may be there for different reasons, however, that will
Displaying images using the Shoes GUI toolkit is as simple as using the image method. This tutorial walks you through adding images in Shoes.
Getting a list of changes is useful, but what about getting a diff of what exactly was changed? A diff is a listing of all parts of a file that differ. So, for example, it'll tell you that you removed three lines and added 4 new lines. It's a way of both seeing what was changed and sending those changes to other developers without having to send them the entire file.
Learn to start writing Ruby code using Test-Driven Development.
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.
In RVM-speak, a "Ruby" is a version of Ruby either installed or it knows has to install. Before you can
What is a regular expression and how do you use them in Ruby?
You may have heard of Test-Driven Development, but what does it really mean? Learn the reasons for using TDD and the features of it.
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.
JSON is a data serialization format for communicating between applications. It's a text format that features a lightweight syntax and closely mirrors the common data structures in modern programming languages.
Base64 is a way to represent any data as ASCII data. Ruby provides a few handy functions to encode and decode Base64.
If you're coming from a compiled language, it's easy to think that method calls are like native function calls. This is simply not the case in Ruby.
How do Ruby and Perl compare in function and form?
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.
Global variables are not often used in Ruby, however, you should at least understand their usage should you ever encounter them.
While dynamic programming and duck typing often make it a bad idea to do so, it's sometimes useful to do different things based on the type of a given variable.
Using GeoIP databases to correlate IP addresses and physical locations using the GeoIP gem.
The Ruby on Rails 3 Tutorial by Michael Hartl is the antithesis of the rapid screencast approach. In the tutorial, the reader is walked through how to use Rails slowly and now with the live lesson video instructions Hartl has applied this philosophy to screencasts. They slow things down and show how to do things visually, and over the 18+ hours of video included they will show you how to use Rails from the ground up.
Part two of the Ruby on Rails blog tutorial focuses on using the Yahoo! UI library to help make your blog look like a blog.
It's often useful in games, simulation and unit testing to generate random numbers, Ruby provides easy access to a pseudo-random number generator.
Learning Ruby isn't often thought of as a Zen exercise, but that's exactly how the guys at EdgeCase saw it. Thus RubyKoans was born.
Using the RubyGem 'cheat' for access to wiki-like cheat sheets.
The release of Ruby on Rails 2.2 bring a new set of features to the table. While release 2.2 is only an incremental release and there are no major architectural changes, there are still some important thing you should know about.
CSRF attacks on your site can also occur in conjunction with XSS breaches. The severity of the CSRF attack is dependent on the severity of the XSS vulnerability.
The following is the complete code for the "CSV Parsing" articles you can find here . As noted in the
Quantifiers tell how many times an element should be matched. Below is a list of all quantifiers supported by the Ruby regular expression engine, how to use them and examples of their use. Though you can get by with the common quantifiers such as + or ?, there are a few more you should know about.
A POST request is used to submit forms or upload files.
A review of the book Beginning Ruby: From Novice to Professional by Peter Cooper from Apress.
This tutorial is a introductory look at the Shoes GUI toolkit for Ruby. You'll find it is easy to learn, making it a good option for beginning programmers to make Shoes online applications.
The Test::Unit library comes with a variety of assertions to make writing tests much easier. Learn what the assertions are for Test-Driven Development and when to use them.
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.
The NetBeans IDE created by Sun Microsystems is often associated with Java development. However, in the past few years, Sun has slowly been adding support for more languages to NetBeans. Ruby (and more specifically, JRuby) is one of those languages.
Digraphs are extremely useful for hierarchical data visualization. However, they're not very easy to generate yourself. This is where Graphviz and the Graph gem come in.
By checking if the server from which the request originates is the same as the server your application runs on, you can determine if the request was generated from your site or not. If an HTTP request lacks a Referer header you can also tell it was generated by hand or from a location where Refererwould make no sense (such as an email client).
wxRuby, a GUI toolkit for Ruby which interfaces with wxWidgets, can be installed on Ubuntu Linux with relative ease.
Now that you have a handle on the Twitter API and parsing the data returned by Twitter, it's time to start tweeting!
The basic syntactical elements of regular expressions in Ruby.
Exploring and knowing the Twitter API is fine, but what do you do from there? Here are 3 practical scripts to help you follow followers on Twitter, update your Twitter account via an RSS feed and to be notified of your friend's new Tweets as they come in.
How to use conditional statements in Ruby
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.
First step to any game: create a window for drawing. This will also initialize the Gosu library, so it'll be ready to poll for input, play sound, etc. This article will also introduce the basic framework for games that will be used in all further articles.
Games are, by definition, interactive. Gosu makes this interaction straightforward with a simple interface for detecting and reacting to key and mouse button presses.
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.
Shortening URLs for Twitter is much easier with the one-line interface provided by ShortURL.
Some problems seem difficult, there are just too many things to consider and it's going to become a mess. However, if you analyze it, there's often a single data structure that will make the whole thing easier.
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.
To oversimplify a rather complex topic, a game is a program that loads images and displays them at the screen. So vital to game programming, loading and displaying images is one of the first things you should learn. Gosu does a pretty good job of making this easy, to a certain degree.
CSRF attacks are similar in nature to Cross-Site Scripting (XSS) attacks and can occur in conjunction with an XSS attacks, but they don't always go hand-in-hand and differ in nature. While XSS attacks rely on HTML code injected into the target site, a CSRF attack may also be initiated from a second site.
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.
Variables are named references to objects in memory. Their several variants (local, instance, class, constant, etc) each have their own subtleties.
A listing of the named character classes available to you in regular expressions in Ruby.
This article was written for Bundler version 1.0. If you are using a later version of Bundler, the commands
Generating cryptographic hashes in Ruby is quite easy. Ruby provides both a built in method and gives access to OpenSSL's hashing algorithms in most installations.
There are two basic things you can do with an object: call a method on it and access an attribute. Ruby doesn't make a distinction between these things, but it does give you some tools to make some methods act like attributes.
The base assertion in the Test::Unit library. assert_equal will not fail as long as its two arguments are equal.
Any value associated with a key identifying it.
One of Rack’s greatest features is the ability to easily plug in middleware applications.
A visual walkthrough on installing Ruby on Windows.
Garbage collection is the process of freeing unused objects. It's a hallmark of many high level languages, including Ruby.
An ordered collection of values, often assigned to a variable. In Ruby, the array takes the place of all ordered collections. They act as arrays, lists, stacks, queues, etc.
You can learn Ruby for free with this online tutorial, Try Ruby! A quick profile of why the lucky stiff's in-your-browser Ruby lessons for beginners.
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.
Exploitation of a website utilizing the trust the site has for a certain user.
Adding foreground and background colors to your Shoes applications is easy. Shoes has a number of built-in color names that mirror the X11 Color Names used on Linux systems. You can also use HTML-style RGB hex values. Here's how to do it.
How to use some of the most basic Tk widgets.