You'll Be Seeing Ruby
An Introduction to the Ruby Programming Language
Mission Data
Chuck Fouts and Darren Day
You'll Be Seeing Ruby...
- in scripting situations, where you'd see bash and perl
- powering web applications on the server side
- soon
What is Ruby?
- An object-oriented scripting language
- Perl-like in the toolset and problems it was intended to address
- Smalltalk-like in its fundamental OO design principles
I, as a language maniac and OO fan for 15
years, really really wanted a genuine object-oriented, easy-to-use
scripting language.
- Matz, [ruby-talk:00382] Re: history of ruby
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/382
Very Brief Ruby Timeline
- 1993: Yukihiro "Matz" Matsumoto begins development on Ruby
- 1995: Ruby officially released in Japan
- 1998: Matz's first official Ruby announcement in English on comp.lang.misc
- 2000: Programming Ruby: A Pragmatic Programmer's Guide, by Dave Thomas and Andy Hunt, is published
- 2004: 2nd Edition of Programming Ruby, now referred to as "the Pickaxe book"
Ruby Fundamentals
The Principle of (Matz's) Least Surprise
Understand these fundamentals, and not much in Ruby will surprise you.
- Interpreted execution
- Everything (that can be assigned to a variable) is an object
- Variables are dynamically typed
- Objects respond to method calls, returning object responses
- Blocks of code can be passed as a parameter in method calls
Try Ruby without Ruby
tryruby.hobix.com
- interactive Ruby environment with built in tutorial mode
- think of it as IM session with Ruby
- created by why the lucky stiff, enigmatic Rubyist
Ruby Releases
- The current release is 1.8.x (1.8.4 as I write this)
- Current development release is 1.9.x
- See the pattern? Even numbers (1.8.x) are production releases
- Odd numbers are development releases
What Do You Get?
- ruby: executable command interpreter
- irb: interactive Ruby shell
- ri: ruby information; think manpages for classes
- rdoc: ruby documentation compiler; think javadoc for ruby
- erb: templating engine for ruby; think jsp
- the standard class library: ranging from Object, Hash, and Array to Zlib
Default Library
- By default, Ruby includes many classes
$ ruby -e 'puts Module.constants.join(" ")'
ARGF ARGV ArgumentError Array Bignum Binding Class
Comparable Continuation Data Dir ENV EOFError Enumerable Errno
Exception FALSE FalseClass File FileTest Fixnum
Float FloatDomainError GC Hash IO
IOError IndexError Integer Interrupt Kernel LoadError LocalJumpError
Marshal MatchData MatchingData Math Method Module NIL NameError
NilClass NoMemoryError NoMethodError NotImplementedError Numeric Object
ObjectSpace PLATFORM Precision Proc Process RELEASE_DATE RUBY_PLATFORM
RUBY_RELEASE_DATE RUBY_VERSION Range RangeError Regexp
RegexpError RuntimeError STDERR STDIN STDOUT ScriptError SecurityError
Signal SignalException StandardError String Struct
Symbol SyntaxError
SystemCallError SystemExit SystemStackError TOPLEVEL_BINDING TRUE Thread
ThreadError ThreadGroup Time TrueClass TypeError UnboundMethod VERSION
ZeroDivisionError
Standard Library
- Many useful classes ship with Ruby, but must be included specifically in your program with the
require command.
abbrev base64 benchmark bigdecimal cgi complex csv curses date dbm
delegate digest dl drb English enumerator erb etc fcntl fileutils
finalize find forwardable ftools gdbm generator getoptlong gserver
iconv importenv io/wait ipaddr jcode logger mailread mathn matrix
mkmf monitor mutex_m net/ftp net/http net/imap net/pop net/smtp
net/telnet nkf observer open-uri open3 openssl optparse ostruct
parsedate pathname ping pp prettyprint profile profiler pstore
pty racc racc/parser rational rdoc readbytes readline resolv
resolv-replace rexml rinda rss runit scanf sdbm set singleton soap
socket stringio strscan syck sync syslog tcltklib tempfile test/unit
thread thwait time timeout tk tmpdir tracer tsort un uri weakref
webrick Win32API win32ole wsdl xmlrpc xsd yaml zlib
irb
- irb is the interactive Ruby shell
- like a conversation with the Ruby runtime
- type a line of code and see the results immediately
- you'll see lots of
nil returns from calls with no return value, like puts
irb(main):001:0> puts "Hello Ruby"
Hello Ruby
=> nil
irb(main):002:0> 3 + 5
=> 8
ruby command
- ruby interpets a file's (or stream's) worth of Ruby code
- supports shebang (
#! /usr/bin/ruby) syntax in *nix shell commands
- ruby -e [command] on the commandline
ruby hello.rb
howdy there
ruby -e "a = 3" -e "b = 5" -e "puts a + b"
8
Anatomy of an .rb
require any other Ruby files needed
require searches a standard set of paths for .rb files and object extensions
- multiple
require calls load the file once
- get down to business and write your codes!
require 'set'
set = Set.new
[1,2,2,3,4,5,1,2,3,5,5].each { |item| set.add item }
puts set.size # what? no Hello World?