Mitch Richling: Ruby Example Programs
Author: | Mitch Richling |
Updated: | 2023-11-15 |
Table of Contents
- 1. Introduction
- 2. Process Environment
- 3. Data Structures
- 4. Variable Types and Scope
- 5. Functions
- 6. Classes
- 7. Comments & Documentation
- 8. Language Control Structures
- 9. Databases
- 10. DRB
- 11. Files & I/O
- 12. Directories & Files
- 13. Forking & Process Stuff
- 14. Signals
- 15. OpenSSL
- 16. Random Numbers
- 17. Rinda
- 18. Time
- 19. GUI
- 20. Web Stuff
- 21. CGI
- 22. Misc
1. Introduction
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!
Get it all on github: https://github.com/richmit/ex-ruby/
2. 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.
3. 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.
4. Variable Types and Scope
types01.rb
- Simple type concepts in Ruby.
varNameAndScope.rb
- Variable names and scope Ruby.
varScopeAndBlock.rb
- Local variables in blocks.
5. 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.
6. 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.
7. Comments & Documentation
comments.rb
- Comments in Ruby.
rdoc01.rb
- How to use RDoc usage for -h options.
8. 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.
9. 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).
10. DRB
drbClient.rb
- Simple DRb client.
drbServer.rb
- Simple DRb server.
11. 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.
12. Directories & Files
dirAndGlob.rb
- Simple stuff with directories in Ruby.
findFile.rb
- Demonstrate the Ruby find module.
find.rb
- Demonstrate the Ruby find module.
13. 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.
14. 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.
15. OpenSSL
opensslCrypt.rb
- Ruby OpenSSL example using Cypher.
opensslDigest.rb
- Ruby OpenSSL example using Digest.
16. Random Numbers
randomNumbers.rb
- Generate random numbers with Ruby.
random.rb
- Generate random numbers with Ruby.
17. Rinda
rindaClientNot.rb
- Rinda client program.
rindaClientSmpl.rb
- Rinda client program.
rindaServer.rb
- Rinda server program.
18. Time
timeHighRes.rb
- High resolution timers in Ruby.
timeSimple.rb
- Simple use of the Time class.
19. GUI
tkHello.rb
- Hello world program with Ruby using Tk.
rTk.rb
- Hello world program with Ruby using Tk.
20. 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.
21. 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.
22. 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