Beruflich Dokumente
Kultur Dokumente
Psalm Mizuki
Summary
Background
What Ruby is Good for
Design Principles of Ruby
Some Features of Ruby
Surprising/Confusing Things in Ruby
A Little Background
Originatedin Japan during the 1990’s
Developed and designed by Yukihiro
Matsumoto
Based on Perl, Smalltalk, Python, and
CLU
– Multiple paradigms
Things Ruby is Good for
Text Processing
– File, String, and Regexp classes
CGI Programming
– Text-handling classes
– CGI library
– DB interface
– Embedded Ruby and mod_ruby for
Apache
Things Ruby is Good for
Network Programming
– Well-designed socket classes
GUI Programming
– GUI toolkit interfaces
Ruby/Tk
Ruby/Gtk
XML Programming
– Text-handling features
– UTF-8-aware regex engine
– Interface to expat XML parser library
Things Ruby is Good for
Prototyping
– High productivity
– Prototypes can become production systems by
replacing bottlenecks with C-written extensions
Programming Education
– Based on a variety of different languages
– Considered by some as a scripting language, so
people can quickly start to write programs
Design Principles
Principle of Conciseness
Principle of Consistency
– Principle of Least Surprise
Principle of Flexibility
Principle of Conciseness
“Iwant computers to be my
servants, not my masters. Thus, I’d
like to give them orders quickly. A
good servant should do a lot of work
with a short order.”
Principle of Consistency
Uniform object treatment
Small set of rules covers all of Ruby
Principle of Least Surprise
Someone with a basic knowledge of
programming languages can learn
Ruby quickly
Principle of Least Surprise does not
mean that no one will ever be
surprised by anything that they see
in a language
Principle of Flexibility
Ruby has a relatively small amount
of syntax that is unchangable, and
pretty much everything else is done
in extensible class libraries
User-defined objects can be treated
like built-in ones
Features
Object-oriented design
Classes with inheritance
Mixins
Iterators
Closures (Blocks)
Garbage collection
Features from Smalltalk
Dynamic: no static type information used
Purely object-oriented: all values are
objects, including classes
– All procedures are a method of some object,
even things that look like function calls
Designed to be object-oriented from the
outset (unlike Perl)
Has garbage collection
Differences from Smalltalk
No really weird syntax
Operators follow usual operator
precedence (2+3*4 == 2+(3*4))
There is separation between the
programming language itself and its
interpreter
Features from Perl and
Python
Considered in some ways a scripting
language
– Support a fast development cycle because
compilation isn’t needed
– Require less code to get things done
– Strong set of built-in libraries that supports text
and file handling
Syntax
Portable
Free
Differences from Perl
Fewer sigils
Less context-dependent
Fewer implicit type conversions
While most Perl functions exist in
Ruby, they are organized into class
libraries
Slightly slower, but more concise
Differences from Python
Don’t use indentation to structure
code
No conversion between small and
large integers
Don’t need to maintain reference
counts in extensions
More syntax
Faster
Features
Object-oriented design
Classes with inheritance
Mixins
Iterators
Closures (Blocks)
Garbage collection
Inheritance and Mixins
Ruby uses single inheritance to avoid
problems with multiple inheritance
Uses mixins to get something similar
to multiple inheritance, without the
problems that can be introduced by
multiple inheritance
Inheritance and Mixins
Mixin: similar to an interface in Java,
but you provide the implementation
Have no explicit superclasses
Cannot be instantiated
Also called modules (actually type
module when defining a mixin in
Ruby code)
Features
Object-oriented design
Classes with inheritance
Mixins
Iterators
Closures (Blocks)
Garbage collection
Iterators
Don’t use loops to iterate over objects
– Only loops that are implemented in Ruby
are the while loop and until loop
Objects are in charge of their own
traversal
– This avoids errors with indices
Use code blocks to customize behavior
of iteration
Features
Object-oriented design
Classes with inheritance
Mixins
Iterators
Closures (Blocks)
Garbage collection
Closures (Blocks)
A block is code that is either found
between curly braces or between a do and
end
Similar to anonymous methods
Can be passed around as parameters and
stored in variables
Great for transactions
Inspired by Lisp
Features
Object-oriented design
Classes with inheritance
Mixins
Iterators
Closures (Blocks)
Garbage collection
Garbage Collection
Uses a mark and sweep method of
garbage collection
Scans C stack and registers
– This way extensions don’t need to
maintain reference counts or protect
local variables
No INCREF/DECREF and no mortal
Some Surprising Things
Duck/Dynamic typing
Scoping
Truth
Open classes
Duck Typing
Ifit walks like a duck and quacks like
a duck, then it must be a duck
Define an object by what it can do,
not its type
Powerful for prototyping
– Can customize as development
progresses
Scoping
Use sigils to determine the scope of a
variable
Can make things slightly awkward if
you’re used to Perl
Scoping
Scope Prefix/Sigil Example
Global variable $ $global
Instance @ @instance
variable
Local variable Lowercase or _ local/_local