Mixins give you the power of multiple inheritance without the headaches Modules allow addition of behaviors to a class Reflection is built in along with lots of other highly dynami
Trang 1Ruby lecture
Trang 2 Write more understandable code in less lines
Free (Very open license)
Trang 3 Dynamic programming languages are often
referred to as 'weakly typed' which means
that variables are not bound to a particular
type until runtime
It typically features "dynamic typing," which gives the programmer more freedom to pass parameters at runtime without having to
define them beforehand
This dynamic typing is accomplished by using
an interpreted language, rather than a
compiled language
Dynamic Programming
Language
Trang 4 A scripting language is a programming language that allows control of one or more software
applications "Scripts" are distinct from the core code
of the application, which is usually written in a
different language, and are often created or at least modified by the end-user Scripts are often
interpreted from source code whereas the
applications they control are traditionally compiled.
of the performing arts, in which dialogue is set down
to be spoken by human actors.
A Scripting language
Trang 5 interpreted (no linking or compilation)
less efficient to run
faster to develop (factor of 5 to 10)
high level – a single statement is more powerful
no type declarations, typeless
powerful built-in types
can generate and interpret source code at run time (via eval)
interface to underlying operating system – can call
operating system commands
plays well with others – glue systems together
rarely used for complex algorithms or data structures.
Examples: javascript, perl, php, smalltalk, ruby, vbscript, groovy, emacslisp, awk, python, tcl, unix shell.
Characteristics of Scripting Language
Trang 6Japanese Design Aesthetics Shine Through
elements of an interface conflict, or are
ambiguous, the behavior should be that
which will least surprise the human user or programmer at the time the conflict arises.
Principles of Ruby
Trang 7The supreme design goal of Ruby
Makes programmers happy and makes Ruby easy to learn
Examples
What class does an object belong to ? – can ask
o.class (returns the class object)
Is it Array#size or Array#length?
Either one - same method – they’re aliased
Is this good or bad design?
Array operators? – if it makes sense, they are defined.
diff = ary1 – ary2 (removes elements of ary2 from ary1)
union = ary1 + ary2 (concatenate arrays)
intersection = ary1 & ary2 (elements common to both)
The Principle of Least Surprise
Trang 8also known as the Principle of Least EffortMotivation…
We don’t like to waste time
The quicker we program, the more we
accomplish
Sounds reasonable enough, right?
Less code means less bugs
Principle of Succinctness
Trang 9All classes derived from Object including Class (like Java) but there are no primitives (not like Java at all) EVERYTHING is an Object.
Ruby uses single-inheritance
What are the dangers of multiple inheritance?
Mixins give you the power of multiple inheritance without the headaches
Modules allow addition of behaviors to a class
Reflection is built in along with lots of other highly dynamic metadata features
Things like ‘=‘ and ‘+’ that you might think are
operators are actually methods (like Smalltalk)
Ruby is Truly Object-Oriented
Trang 10 a mixin is a class that is mixed with a
module In other words the implementation
of the class and module are joined,
intertwined, combined, etc
a module as a degenerate abstract class A
module can’t be instantiated and no class can directly extend it but a module can fully
implement methods
Mixin
Trang 11# Convert a integer value to English.
The class that will
be mixed with this module needs to define and set a
@value instance variable
a module could invoke methods defined not in the module itself but in the class that it will
be mixed with.
Trang 12# A Math module akin to Java Math class.
2.module Math
3. # Could be called as a class, static, method
4. def add(val_one, val_two)
5. BigInteger.new(val_one + val_two)
6. end
7.end
Module (self contained)
and so can be class method.
Trang 13# Base Number class
08.# BigInteger extends Number
09.class BigInteger < Number
10.
11. # Add instance methods from Stringify
12. include Stringify // mix methods at the instance level
13.
14. # Add class methods from Math
15. extend Math // mix methods at the class level
Trang 14# Call class method extended from Mathbigint2 = BigInteger.add(-2, 4)
puts bigint2.intValue # > 2
# Call a method included from Stringifyputs bigint2.stringify # > 'Two'
Using it…
Trang 15# Format a numeric value as a currency
Trang 16 Method Chaining – applies methods from left to right
print array.uniq.sort.reverse
Method Names include ! and ?
ary.sort!
? returns a true or false
◦ examples: defined? equal?
! changes the object that calls the method (do it here!)
Iterators and Blocks vs Loops
files.each { |file| process(file) }
Case usage:
◦ Class names begin with a Capital letter
◦ Constants are ALL_CAPS
◦ Everything else - method call or a local variable
Convention in naming: Under_score instead of camelCase
Some Coding Conventions
Trang 17songList.each do |aSong|
aSong.play
end
Iterators
Trang 18 Duck Typing
Based on signatures, not class inheritance
In Ruby, we rely less on the type (or class) of an object and more on its capabilities
Duck Typing is a type of dynamic typing in which the object’s current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class
or implementation of a specific interface.
The name of the concept refers to the duck test, attributed to James Whitcomb Riley which may be phrased as follows:
"when I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck."
Dynamic Programming
Trang 19 # Check whether the object defines the to_str method
puts ('A string'.respond_to? :to_str) # => true
puts (Exception.new.respond_to? :to_str) # => true
puts (4.respond_to? :to_str) # => false
The above example is the simplest example of Ruby's philosophy of
"duck typing:" if an object quacks like a duck (or acts like a string), just go ahead and treat it as a duck (or a string) Whenever possible, you should treat objects according to the methods they define rather than the classes from which they inherit or the modules they include.Duck Typing
Trang 20Duck Typing Example
If you are calling
“quack”, Duck and DuckRecording are interchangeable
If you are calling
“swim”, Duck and Goose are
interchangeable.
Method isn’t expecting a specific type JUST one with the needed
functionality
Trang 21 Dynamic Dispatch
A key concept of OOP: methods are actually messages that are
sent to an object instance
It is a runtime decision to decide which code to execute
Can’t be determined statically (method could have been
overridden dynamically)
foobar = Array.new
foobar.class # => Array
foobar.size # => 0
Conceptually: First we search for “size” in Array If it isn’t there, we look to Object
to define size If the method isn’t found at the top of the hierarchy, we have a
“method_missing” message.
Dynamic Language
Trang 22The regular search path:
The new search path
foobar = Array.new
def foobar.size // Adds a size method ONLY for the instance
“Infinity and beyond" ;
end
Trang 23 Languages with weak or no typing systems often carry a dispatch table as part of the object data for each object This allows
instance behavior as each instance may map a given message to a separate method
Languages with strong (static) typing use a virtual table which defines method to code mapping Instances of the type store a
pointer to this table
Dynamic Language
Implementation
Trang 24 Suppose a program contains several classes in
an inheritance hierarchy: a superclass Cat, and two subclasses, HouseCat and Lion Class Cat defines a virtual function named speak, so its subclasses may provide an appropriate
implementation (e.g either meow or roar)
When a Cat variable calls speak, we need to be able to tell which method to call
dispatch table contains addresses of methods
Use same layout for type-compatible methods – so we look for address at a constant offset
Dynamic Dispatch in C++
Trang 26 One of the many advantages of dynamic languages such as Ruby is the ability to
introspect -to examine aspects of the program from within the program itself Some call this feature reflection
We might discover:
◦ what objects it contains,
◦ the current class hierarchy,
◦ the contents and behaviors of objects, and
◦ information on methods
Reflection
Trang 27 Everything is an object EVEN the class.
Trang 28 Have you ever craved the ability to traverse all the living objects in your program? Ruby lets you perform this trick with ObjectSpace::each_object
For example, to iterate over all objects of type Numeric, you'd write the following
Hey, where did those last numbers come from? We didn't define them in our
program The Math module defines constants for e and PI; since we are examining all living objects in the system, these turn up as well
Looking at Objects
Trang 29To get ObjectSpace to work from Netbeans use option –X+O (O as in Oreo)
Right click on Project name,
Select Properties
Click Run
Define Ruby Option
Trang 30 For instance, we can get a list of all the methods to which an object will respond
Trang 31puts self.class # scripts are automatically in Object
puts 3.class # numbers are Fixnum
Trang 32puts eval trane
puts eval miles
produces 14
M Davis
14
M Davis
Method can be passed around as objects (%q is
a single quoted string)
Trang 33eval ("puts @amt", $here) # 35
eval ("puts @amt” ) # nil
Scope
At instance variable begins with @ and its scope is confined to
whatever object self refers to
A global variable begins with $
It can be referred to from anywhere
in the program
Trang 34eval ("puts @amt", $here) # 35
eval ("puts @amt” ) # nil
eval "@amt=23.92", $here
eval "puts @amt", $here # 23.92
Scope
We can even modify variables in original scope
Trang 35 One nice feature of globals, is that they can
be traced; you can specify a procedure which
is invoked whenever the value of the variable
is changed
trace_var :$here, proc{print "$here is
now", $here, "\n"}
Tracing…
Trang 37 caller is a Ruby method of the Kernel class, which all objects inherit
It returns an array of strings representing the call stack
Getting the call stack
Trang 38k1.aC # prints call stack
C\cs4700\simple_ruby_application\lib\ Scoping.rb:25:in `aB'
C:\cs4700\simple_ruby_application\lib
\Scoping.rb:28:in `aC'
C:\cs4700\simple_ruby_application\lib
\Scoping.rb:49
Trang 39 Java features the ability to serialize objects,
letting you store them somewhere and
reconstitute them when needed You might use this facility, for instance, to save a tree of objects that represent some portion of application state, save it, and recreate is later.
Ruby calls this kind of serialization marshalling.
example) in line for a parade, maneuver, or
review.
Can be used to create a distributed object
system.
Marshalling
Trang 40File.open("posterity", "w+") do |f| Marshal.dump(c, f)
end
File.open("posterity") do |f|
chord = Marshal.load(f) puts chord.play #» G-Bb-Db-E end
Trang 41 the format used by Marshal has changed a few times
in the past
it's Ruby-only AFAIK nothing else can read data
serialized with Marshal.
serializing objects with Marshal exposes
Trang 42Using Ruby's alias it is possible to reopen a class, override a method and still call the original.
Clock Radio is complete,
Sometime later we can redefine methods and add new ones:
Trang 43 The advantage you gain from being able to
reopen classes is that you can patch the
framework you’re using unobtrusively in order
to fix bugs or improve performance
Advantages
Trang 44 Violates OO principle by allowing anyone to add
members and methods to an existing class, even
outside of the original class definition
The methods thus added have full access to all other members and methods, including private ones
This is a “violation of the Open/Closed Principle,” in that the original class is not kept closed against
modifications
It certainly isn’t pure There really isn’t a Grand
Unifying Idea It clearly copies features from very
different languages and allows for a variety of
programming styles
Scope reopening OO criticism
Trang 45A closure is a nameless function.
It has code to run (the executable)
It has state around the code (the scope)
You capture the environment (the local variables) in the closure.
Even after the function has returned, and its local scope has been destroyed, the local variables remain in
existence as part of the closure object
Local variables are shared between the closure and the method It's a real closure It's not just a copy (This is a Perl feature that is copied.)
Closure
Trang 46 You can reconvert a closure back into a block, so a closure can be used anywhere a block can be used
Often, closures are used to store the status of a
block into an instance variable, because once you convert a block into a closure, it is an object that can be referenced by a variable
And of course closures can be used like they are used in other languages, such as passing around the object to customize behavior of methods
Closure – what can you do with them?
Trang 47The important thing to remember about Ruby is that there isn't a big difference between
``compile time'' and ``runtime.‘’ This allows:
adding code to a running process
redefining methods on the fly, change their scope from public to private, and so on
altering basic types, such as Class and
Object
Compile Time? Runtime?
Anytime!
Trang 48 evaluating math expression (like in spreadsheet)
more flexible than a parser as can call functions that were just created.
implemented with the same interpreter as normal code
string, and it’ll evaluate it.
Characteristic of scripting languages
Trang 49 input = # read from some formeval input
What if they input 'rm -rf *‘ ?
Slow – string must be parsed
Eval
Trang 50- Side Effects: As an everyday example, the statement
x = gets
reads a line and sticks it into x just like you’d probably expect, but it also has the side effect of sticking the line into the global variable $_.
Ruby also blurs the line between names of variables, classes,
and methods. Some languages allow the same identifier to be both a class and a variable, but context distinguishes Ruby
does not.
Ruby delights in the bizarre usage of operator overloading
”<<” normally means “left shift”, but it also means ” here
Other operators have similar illogical overloadings; for example, the ”<" operator not only means "less than" but also "is a
subclass of."
Criticisms of Ruby