Software Development
with Ruby
by Teo Choong Ping
#!/usr/bin/ruby
puts "Hello World"
Ruby background and history
● Created by Yukihiro Matsumoto a.k.a “Matz”
● Started on February 24, 1993
● First public release in 1995
● Latest version is 1.8.4
● Version 1.9 in active development
● Ruby is 11 years old and very mature
What is Ruby?
"an interpreted scripting language for quick and
easy object-oriented programming"
* powerful string operations,
* regular expressions
* everything is an object
* classes, inheritance, methods, mixin
* iterators and closures
* variables are not typed
* syntax is simple and consistent
* garbage collection
* multiple precision integers
* exception handling
* threads
Adapted from http://www.ruby-doc.org/docs/UsersGuide/rg/whatisruby.html
(personal experience)
- from Perl and Java background
- advocates and practices Agile development
- first learn Ruby 2 years ago
- first Ruby GTK application in few hours
- first Ruby on Rails app in few hours
Ruby in 10 minutes!
● Loop
● Array
● Hash
● IO
● Subroutine
● OOP
Ruby in 10 minutes!
Loop
i=0
while i < 3
print i, "weee \n" Output:
i += 1 weee
end weee
weee
for n in 0...3
print n, "weee \n"
end
Ruby in 10 minutes!
Array
# array = Array.new()
# array = []
array.push('one')
array.push('two') Output:
array[2] = 'three' one
two
(0..array.length()).each do three
|n|
print "#{n} \n "
end
array.each{ |n| print "#{n} \n"}
Ruby in 10 minutes!
Hash
hash = {}
hash = Hash.new()
hash = {'name' => 'sey', 'e-mail' => 'seymores@gmail.com'}
hash['language'] = "Ruby"
hash.each{|key, value| print key, "=>", value, "\n"}
print "#{hash['name']} \n"}
Output:
name => sey
e-mail => seymores...
language => ruby
sey
Ruby in 10 minutes!
IO
file = File.new('/etc/resolv.conf', 'r')
arrayLines = file.readlines()
i=0
arrayLines.each{|line| printf("Element %3d: %s\n", i, line.chomp); i+=1}
file.each{|line| printf("Line %3d: %s\n", i, line.chomp); i+=1}
file.close()
print "Enter favorate language==>"
language = gets()
print "\n\nYour favorate language is ", language, "\n"
Ruby in 10 minutes!
Subroutine
def say_hello_world(name=”anonymous)
print “Hello World, #{name}”
end
say_hello_world(“sey”)
Output:
Hello World, sey
Ruby in 10 minutes!
OOP
class Customer < Person
attr_accessor :name, :occupation
def initialize(name, occupation)
@name = name
@occupation = occupation
end
end
me = Customer.new(“Seymour Cakes”, “Programmer”)
me.name
me.occupation
What can Ruby do?
● Web Application
● CGI, Ruby on Rails, etc
● GUI Windows Application
● GTK2, QT, Tk, Wx, Fox
● Database
● DBI, dbm, ActiveRecord
● Web Service
● REXML, SOAP4R, Active Web Service
● Microsoft Windows Integration
● Win32API, win32ole
● Network Programming
● Socket
(and more ...)
● build-in debugger, profiler, benchmark
● Test Driven Development (TDD) friendly
● easy deployment with Ruby Gem
● easy build with Rake (similar to Apache Ant)
● Runs on Windows, Linux, *BSD, Solaris, MacOS X
IRC client in 1 minute
require 'socket'
client = TCPSocket.new('kornbluth.freenode.net', 6667)
client.send("NICK sey\r\n", 0)
client.send("USER sey kornbluth.freenode.net hello :SeymourCakes\r\n", 0)
client.send("JOIN #myoss\r\n", 0)
client.send("PRIVMSG #myoss :Hello everyone\r\n", 0)
print client.recvfrom(50)
client.close
10:26 -!- rubysey [n=rubysey@xxxxxx.com] has joined #myoss
10:27 < rubysey> Hello everyone
10:27 -!- rubysey [n=rubysey@xxxxxx.com] has quit [Remote closed the connection]
GTK2 Application in 2 hours
# Main program
basedir = File.dirname(__FILE__)
PROG_PATH = basedir + "/programmeralarm.glade"
PROG_NAME = "Programmer Alarm"
Gtk.init
ProgrammeralarmGlade.new(PROG_PATH, nil, PROG_NAME)
Gtk.main
● 440 lines of XML
● 145 lines of Ruby
● 585 total lines of code
● About 2 hours of work
● bzzz.rubyforge.org
The Future of Ruby...
● Web 2.0
● RAD GUI Database Application
● Framework development
● Distributed application
How to get started.
● Download and install Ruby
● Google for newbie tutorials
● Go to Ruby-doc.org for more docs
● Read Ruby codes from Rubyforge.org
● Read Programming Ruby, 2nd Edition
● Join freenode #ruby-lang
● Write Ruby codes!
Thank you!
shitmores.blogspot.com