Upload
mervin-matthews
View
212
Download
0
Embed Size (px)
Citation preview
Ruby
Quick History
Name chosen because it was the birthstone of the colleage of the creator Yukihiro Matsumoto
First public release Dec 21 1995
Created because Matsumoto wanted“ scripting language that was more powerful than Perl, and more object-oriented than Python."
Ruby is a pure OOP language everything is an object.
Anything and everything can be overloaded
5.times { print "We *love* Ruby -- it's outrageous!" }
Abstract Data Types
Again everything in ruby is an object Integers, booleans, and etc are objects and can
be modified and used as such e.x. int.to_i => int
int.to_int => int
int.floor => int
int.ceil => int
So to create an abstract data type, define a class and then generate an object
Ruby ADT ExampleClass Person_ADT def to_Person(parameter(s)) ...code... end def make(person1, person2) ...code... @person3 = person1 + person2 ...code... end def terminate_Person(person) puts "I'll be back!" endend
Ruby Inheritance
Ruby supports single inheritance, meaning it can only inherit properties from a single class
By default methods are public, though java-like private and protected methods can be defined using the private and protected keywords respectively
Subclass methods can override superclass methods using the def
Ruby Inheritance Examplesclass Bird def preen puts "I am cleaning my feathers." end def fly puts "I am flying." end end class Penguin < Bird def fly puts "Sorry. I'd rather swim." end end p = Penguin.new p.preen p.fly
Ruby Dynamic Binding
Because Ruby allows for the overriding of methods some mechanism is needed to determine which method is being called when multiple methods with the same name exist
Ruby makes use of dynamic dispatching. The calling object is what determines which method is implemented.
In the previous example the method fly() is defined twice and executes differently if a Bird or Penguin object is the calling method
Ruby Subclasses
Once more, everything is an object in Ruby, so rather than having subtypes for subclasses, Ruby just has subclasses that are an extension of the parent object
Also Ruby allows nested classes, you can define classes within other classes. A good example of this is inherited classes in Ruby.
Nested Classes
Ruby allows
Ruby Nested Classes Example
class A < Object
def initialize
@y = 0
end
class B < Object
def initialize
@x = 0
end
def f
# ...
end
end
end