Ruby examples (example source code) Organized by topic

Rails /

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

ActiveRecord /

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

Unit Test /

assert 5Unit Testing 10


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

Network /

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

Threads /

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

Array /

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

Hash /

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

Range /

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

Collections /

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

Number /

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

Method /

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

Class /

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

Reflection /

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

Database /

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

File Directory /

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


rexml 18XML Builder 4xmlparser 1XPath 10

Date /

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

Time /

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

String /

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

Design Patterns /

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


gtk 2opengl 1wxruby 1

Tk /

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

Language Basics /

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

Statement /

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

Development /

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

Windows Platform /

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