Jump to my Home Page Send me a message Check out stuff on GitHub Check out my photography on Instagram Check out my profile on LinkedIn Check me out on Facebook

Ruby Example Programs

When I learn a new language, I write small programs exploring various aspects of the language in question. I find this helps me to work out the details of syntax, and firmly organize the material in my mind. On this page are many of the little programs I wrote while learning Ruby some time ago. I make a point of saying all of this because these programs can, in no stretch of the imagination, be considered examples of good Ruby form -- I wrote them while learning the language after all. Still, they may be of helpful for people just learning the language, and trying desperately to work out some detail... Use them at your own risk!

Process Environment

argParse.rb
Using the 'optparse' module.
arg.rb
Accessing Ruby command line arguments.
env.rb
How to access environment variable in ruby.
termSize.rb
Query the terminal size via ioctl.

Data Structures

arrayAssoc.rb
Associative arrays in ruby.
arrayBasics.rb
Simple array examples.
arrayTraverse.rb
Various array traversal methods.
enumBasics.rb
Some of the Ruby's enumerable goodness.
hashBasics.rb
Simple hash examples in Ruby.
hashOfArray.rb
Building up a hash of arrays.
hashOfCounts.rb
Building up a hash of counts.
hashOfHashAndArray.rb
Traverse a hash of arrays and hashes in ruby.
hashTraverse.rb
Various hash traversal techniques.
setBasics.rb
Sets in Ruby.
strings.rb
Simple string manipulation in Ruby.

Variable Types and Scope

types01.rb
Simple type concepts in Ruby.
varNameAndScope.rb
Variable names and scope Ruby.
varScopeAndBlock.rb
Local variables in blocks.

Functions

funcArgArrayExpand.rb
How to expand arrays into multiple function arguments.
funcArgBlock.rb
Block arguments for ruby functions.
funcArgDefault.rb
Default arguments for Ruby functions.
funcArgNamed.rb
Hack for named function arguments in Ruby.
funcArgVarArg.rb
How to support variable argument counts in Ruby functions.
funcArrExpArg.rb
How to expand arrays into multiple function arguments.
funcBlockArg01.rb
Block arguments for ruby functions.
funcBlockArg02.rb
Block arguments for ruby functions.
funcDfltArg.rb
Default arguments for ruby functions.
funcLocalVar.rb
Function local variable concepts in Ruby.
funcNamedArgs.rb
Hack for named function arguments in ruby.
funcReturnMulti.rb
Returning multiple return values from Ruby functions.
funcReturn.rb
Return values for ruby functions.
funcVarArg.rb
How to support variable argument counts in Ruby functions.

Classes

classAttributeAccess.rb
Attribute, and virtual attribute, access.
classInheritance.rb
Class inheritance in ruby.
classIntrospection.rb
Class introspection in ruby.
class.rb
Simple class concepts in Ruby.
classVariables.rb
Class variables/methods in ruby.

Comments & Documentation

comments.rb
Comments in Ruby.
rdoc01.rb
How to use RDoc usage for -h options.

Language Control Structures

case.rb
Demonstrate the case statement.
caseClass.rb
The case statement with (class) types.
caseInt.rb
The case statement with integers.
if01.rb
if-then construct in Ruby.
loops01.rb
Various loop constructions in Ruby.

Databases

csvEx.rb
Simple example of the CSV module.
dbiPerlLike.rb
Using the Ruby DBI with Perl-like constructs.
dbiRuby.rb
Using the Ruby DBI with Ruby-like constructs.
dbmEx.rb
Ruby DBM example (GDBM actually).

DRB

drbClient.rb
Simple DRb client.
drbServer.rb
Simple DRb server.

Files & I/O

fileIO.rb
Simple file I/O in ruby.
fileNames.rb
Working with file names in Ruby.
fileOperations.rb
Simple file operations with Ruby.
fileStat.rb
File Stat with Ruby.
fileTest.rb
File tests with Ruby.
fileUtils.rb
Demonstrate the fileUtils module.
stdin.rb
Typical way one processes STDIN with ruby.

Directories & Files

dirAndGlob.rb
Simple stuff with directories in Ruby.
findFile.rb
Demonstrate the Ruby find module.
find.rb
Demonstrate the Ruby find module.

Forking & Process Stuff

forkBlock.rb
How to use fork with a block in Ruby.
forkDetach.rb
Basic use of fork.
forkPopenFork.rb
How to fork and popen with child.
forkPopen.rb
How to use ruby popen.
forkShell.rb
How to use perl-like back quote shell escape.
forkSystem.rb
How to use the system() call in ruby.
forkWait2.rb
Basic use of fork and wait2.
forkWait.rb
Basic use of fork and wait.
timeOutEx.rb
Demonstrate the timeout module.
processStuff.rb
Simple process stuff like PID.
procObjects.rb
Procedure Objects.

Signals

signalRecv.rb
Receive UNIX signals.
signalSend.rb
How to send UNIX signals.
signals.rb
Send/receive UNIX signals.
kill.rb
Send/receive UNIX signals.

OpenSSL

opensslCrypt.rb
Ruby OpenSSL example using Cypher.
opensslDigest.rb
Ruby OpenSSL example using Digest.

Random Numbers

randomNumbers.rb
Generate random numbers with Ruby.
random.rb
Generate random numbers with Ruby.

Rinda

rindaClientNot.rb
Rinda client program.
rindaClientSmpl.rb
Rinda client program.
rindaServer.rb
Rinda server program.

Time

timeHighRes.rb
High resolution timers in Ruby.
timeSimple.rb
Simple use of the Time class.

GUI

tkHello.rb
Hello world program with Ruby using Tk.
rTk.rb
Hello world program with Ruby using Tk.

Web Stuff

httpGet.rb
How to act as a HTTP client via Net::HTTP.
webServerMongrel01.rb
Simple web server with Mongrel in Ruby.
webServerMongrel02.rb
Simple web servlet with Mongrel in Ruby.
webServerWEBrick01.rb
Simple web server with Ruby.
webServerWEBrick01s.rb
Simple SSL web server with Ruby.
webServerWEBrick02.rb
Simple web servlet with Ruby.
webServerWEBrick03.rb
Simple web server with Ruby.
webServerWEBrick04.rb
Example illustrating code generation and closures
webServerWEBrickTEST.rb
Test server for HTTP GET and HTTP POST.

CGI

cgiMinimal.rb
Super simple cgi example in Ruby.
cgiParm.rb
Simple CGI example with using Ruby's cgi module.
cgiPost.rb
Dealing with 'file' parameters in CGI.
cgiUploadFile.rb
Dealing with 'file' parameters in CGI.
cgiViaEnv.rb
Simple CGI example with using Ruby's cgi module.

Misc

yamlEx.rb
Using the YAML module.
zlibEx
Example showing typical zlib usage in Ruby.
exception.rb
Exception handling basics in ruby.
getEnt.rb
How to get user password and group info.
msgDigests.rb
Message digests with Ruby.
regex.rb
Basic regular expressions in Ruby.
resolvEx.rb
How to use current host name and DNS info with resolv.
thread01.rb
Simple thread creation in Ruby.
base64.rb
Demonstrate the base64 module
jsonStuff.rb (t1.json)
Using the JSON module.
© 2009 Mitch Richling