Ruby examples (example source code) Organized by topic

Rails /

Action 4Application Framework 1
Controller 1Cookie 1
Environment 1ERB 7
Form Checkbox 1Form RadioButton 1
Form Select 2Form TextField 1
Form 2Model 3
Rails 1Session 1
View 4

ActiveRecord /

Assumption from ActiveRecord 3Connection String 1
Delete 4Insert 1
Map to SQL 6One to Many 2
Primary key 1Search 9
Table Name 6Transactions 1
Update 2

Unit Test /

assert 5Unit Testing 10

CGI /

CGI Form 5CGI Variables 1
CGI 2Cookie 1
HTML 9Parameters 1
Session 2

Network /

action_mailer 2Authentication 1
DNS 2Email POP3 8
FTP Client 14Ftp 1
hpricot HTML Parsing 5HTML Builder 2
HTML Parser 4HTTP Post 5
HTTP Reponse 11HTTPS 5
IMAP 1IP Address 4
mongrel Web Server 2net http library 3
open uri 10ping 4
Proxy 2RSS 2
Ruby remoting 4Server by gserver 4
SOAP 3ssh 4
TCP Server 4TCPSocket 3
Telnet 2UDP Client 1
UDP Server 2URI 10
Webrick Web Server 3XML RPC 2
xmlrpc 8

Threads /

abort_on_exception 2alive 1
critical 1Daemon 3
exit 1fork 6
join 6kill 1
Mutex 2pass 1
priority 2Process 1
raise 1Ruby Threads 4
run 1safe_level 1
SizedQueue 1sleep 5
status 1stop 2
Synchronization 6Thread.main 2
Thread.new 6Thread.pass 1
wakeup 2

Array /

Array Addition 3Array Creation 15
Array Each 3Array Elements 9
Array Extension 16Array Index 11
Array Length 1Array Literal 2
Array Subtraction 1Array.new 7
As Set 11clear 1
clone 1collect 3
combination 1compact 1
Compare Array 5concat 1
count 1cycle 1
delete 2delete_at 1
drop_while 1each 5
each_index 1each_with_index 1
empty 2fill 1
find_all 1find_index 1
first 2freeze 1
grep 4group_by 1
include 3inject 3
insert 1inspect 2
join 3last 2
map 4max 1
max_by 1min 1
Multidimensional Arrays 7Object array 1
pack 2permutation 1
Pop 1Print Array 1
product 1Push 5
Range Index 2reject 2
replace 1reverse 3
reverse_each 2select 1
shift 1slice 4
sort 5step 1
take_while 1to Hash 1
to String 2transpose 1
Two Array Indices 7uniq 3
unpack 1unshift 1

Hash /

assoc 1clear 2
Compare 1Converrt to Hash 1
Convert to Array 1Convert to String 2
default 2default_proc 1
delete 4delete_if 5
each 4each_key 2
each_value 2empty 1
find 1freeze 1
Hash Creation 11Hash Elements 3
Hash Extensions 4Hash Keys 11
Hash Length 1Hash values 3
Hash.new 8has_key 1
has_value 2include 1
index 5inject 2
invert 1Key value 2
member 1merge 4
Nested hash 2Print Hash 4
rehash 1reject 1
replace 3select 1
shift 1sort 2
sort_by 1store 1
update 1values 1
values_at 2

Range /

As Iterator 4collect 1
Convert to Array 2Date Range 4
drop 1each 3
entries 1find_all 1
first 1include 1
inject 2max 1
min 1minmax 1
minmax_by 1multi letter based range 1
partition 1Range Creation 9
Range Extension 2Range include 7
Range Min Max 3Range.new 1
reduce 1reverse_each 1
select 1take 1
to Set 1with case 1
with for 6zip 1

Collections /

Comparable 4Enumerable 10
Forwardable 3Set 41
Your Hash 1Your queue 1
Your stack 1Your tree 8

Number /

abs 2between 1
BigDecimal 12Bignum class 3
Bignum 1Binary number 4
ceil 1coerce 1
Complex Numbers 1Convert from Integer 1
Convert to Char 3Convert to Float 4
Convert to Integer 14Convert to String 8
Fixnum class 6Fixnum extension 5
Float Extension 3float 12
floor 1hexadecimal numbers 3
infinite 4Integer 12
matrix 4nan 5
next 4nonzero 4
Number class 7Number Literal 7
Numeric class 7octal numbers 3
Prime Numbers 6Rational Numbers 16
round 4shift 2
zero 2

Method /

alias 5args 1
binding 2block_given 3
call 1Code Block 16
def 3defined 1
define_method 3lambda 6
Method Arguments 12Method Creation 9
Method objects 1Method Pointer 4
Overloading 1Procs 8
recursive 1remove_method 3
return 10send 2
undef 1Utility Method 6
Variable Number Arguments 5yield 10

Class /

abstract class 2Access level 4
attr 2attr_accessor 5
attr_reader 4attr_writer 3
child class 4Class Defintion 11
Class Methods 4Class Variables 3
clone 3DelegateClass 2
Encapsulation 1eql 2
equal 3freeze 2
getter setter 9include 4
inheritance 10initialize class 5
inspect 2instance variables 5
interface 1Member method 6
Multiple inheritance 2nested classes 2
new 2object Creation 3
Object in Ruby 4OpenStruct 1
Operator 2Overriding Methods 4
Polymorphism 1private 2
Protected 2Public 2
Readable attributes 2self 2
singleton method 4Struct 1
super 2tainted 3
to String 4Virtual method 2
Writable Attributes 2

Reflection /

ancestors 2caller 2
Class Reflection 6class_eval 2
class_variable_get 1eval 3
extend_object 1FILE 1
find_all 1global_variables 1
inherited 1instance_eval 1
instance_method 1instance_of 6
instance_variables 1instance_variable_set 1
is_a 1kind_of 3
local_variables 1Method Reflection 5
method_added 1method_defined 1
method_missing 6Module Reflection 2
module_eval 2Object 7
ObjectSpace 3object_id 4
private_class_method 1private_instance_methods 1
protected_instance_methods 1protected_methods 1
remove_const 1respond_to 5
set_trace_func 1singleton_methods 1
singleton_method_added 3superclass 3
variable Reflection 1

Database /

dbi drivers 1dbm 4
Mysql 1ODBC 1
Oracle 2postgres 2
pstore 1

File Directory /

atime 2basename 2
Binary File 7blksize 1
blockdev 1blocks 1
chardev 1chdir 2
chmod 8chown 1
close a file 1ctime 2
delete 3dev 3
Dir rewind 1Dir.delete 1
Dir.entries 3Dir.foreach 2
Dir.getwd 1Dir.glob 1
Dir.open 2Dir.pwd 1
Dir.reject 2Dir.rmdir 1
Dir 10directory list 2
directory 2dirname 1
each byte 3each line 2
each 2eof 1
Executable.File 1executable_real 1
exist 4expand_path 1
extname 1File class 2
file handle 1File position 2
File SEPARATOR 2File tests 4
File type 1File.exists 2
File.new 5file 1
Find 11flock 1
fnmatch 2ftype 2
gets 3gid 1
grpowned 1identical 2
ino 1IO.close 1
IO.fileno 1IO.flush 1
IO.foreach 2IO.new 3
IO.popen 4IO.puts 1
IO.read 3IO.readlines 2
join 2lock 1
lstat 2mkdir 4
mode 1mp3 file 2
mtime 2nlink 1
Open a File 9owned 1
path 1pipe 1
putc 1puts 2
pwd 2rdev 3
Read lines together 3read 4
Readable File 1readable_real 1
readline 2readlink 1
rename 1seek 2
setgid 1setuid 1
set_encoding 2size 4
socket 1split 1
stat 1sticky 1
StringIO 7subclass File 1
symlink 2sysread 1
syswrite 2tell 1
Tempfile 4Text file 8
tmpdir 2truncate 2
uid 1ungetc 1
unlink 1unmask 1
Utilities 10utime 1
Word counter 4world_readable 1
Writable File 1writable_real 1
write 2zero 1

XML /

rexml 18XML Builder 4
xmlparser 1XPath 10
YAML 6

Date /

ajd 1Date Calculation 11
Date.new 5DateTime 16
downto 1gregorian 1
julian 3mjd 1
next 2parse 3
strptime 4to String 1
to Time 2today 1
upto 1

Time /

Compare Time 2Convert to Date 1
Convert to Integer 1ctime 1
Day Light Saving 5day 1
dst 1gm 2
hour 1local 5
now 3strftime 9
Time Calculation 5Time class 3
Time Extension 3Time format 5
Time Zones 2timer 1
utc 2year 1

String /

capitalize 3casecmp 2
char in string 14char range 4
chomp 3chop 3
concat 2Convert Number 3
Convert to Array 3Convert to String 3
Convert to Symbol 2crypt 1
delete 5downcase 3
dump 1each 2
each_byte 6each_char 3
each_line 1each_with_index 1
empty 1encoding 10
end_with 1eq 1
Escape Characters 14force_encoding 1
freeze 5grep 12
gsub 14hash value 1
hash 1Here Document 3
include 2index 4
insert 3intern 2
Justify Center 5Justify Left 2
Justify Right 4length 2
lines 1match 6
matching operator 2multiply strings 4
next 8replace 3
reverse 5scan 15
select 2slice 3
split 8squeeze 1
String class Extension 20String comparisons 10
String Concatenation 7String format 5
String from Right 1String Literal 9
String Quotation 9String Range 7
string variable 4String.new 5
Strip from Left 2Strip from right 1
Strip 5sub with regular expressions 8
sub 1succ 3
sum 2swapcase 2
tr 2tr_s 1
unicode 5upcase 5
upto 1wrap 2

Design Patterns /

Command 4Composite 5
Decorator 1Factory 2
Iterator 1Observer 4
Singletons 6Strategy 3

GUI /

gtk 2opengl 1
wxruby 1

Tk /

Entry TextField 2Layout 5
Main Window 3TkButton 2
TkLabel 3TkListbox 1
TkRadioButton 1

Language Basics /

Bitwise Operations 5Boolean Operators 9
Boolean variable 2Code statement 2
Comments 5Comparison Operators 5
Constants 7Global Constants 1
Global Variables 2Interpolating 15
irb 4load 2
Math Operators 10Mixins 5
Modules Creation 16nil 5
Operator Precedence 2Operators 2
Parallel Assignment 2Predefined Variables 1
putc 1puts 23
rake 2RDoc 5
require 4Reserved words 5
ri 5Ruby Commandline 3
Ruby Interpreter 5RubyGems 12
shebang line 2STDOUT 2
Symbols 7Ternary Operator 4
Variable scope 5Variables 23

Statement /

BEGIN END 3break 5
case 9catch 2
downto 5ensure 1
for 4if as modifier 5
If 18in 3
include 1loop 1
next 1raise 5
redo 1rescue 6
retry 2step 1
throw 2times 8
unless as Modifiers 2unless 4
Until as modifier 1until 6
upto 10while as Modifier 2
while 8

Development /

ARGF 3ARGV 4
Aspect Oriented Programming 1Benchmark 5
chomp 2Command line 1
csv 10Data Type 2
Embedded Ruby 5Embeded C 1
ENV 2eval 5
Exception 4exec 1
GC 2gem 7
Generator 2gets 8
KCODE 8Kernel 1
Keyboard reading 1logger 4
Marshal 2Match Operator 2
Math extensions 9Math.constants 3
Math 10print 3
printf 5Process 4
profile 2PStore 2
rand 7Regexps Group 5
Regexps Repetition 18Regexps 29
RUBY_PLATFORM 1scanf 1
Shell 1sprintf 5
SyncEnumerator 1system 5
Timeout 2Unicode 1
Utility Regexps 3Your Exception 3
zip 3

Windows Platform /

Dialog 1Excel 5
Internet Explorer 7Powerpoint 1
Sound 1Win32API 5
winsock 1Word 4