Everything in Ruby is an Object
- All entities in a Ruby program are objects
- Even integers and numbers
- All functions are methods
- All computation is based upon name binding (assignment) and sending messages
OO Gives a Great deal of Flexibility
- When software is developed against a protocol
- Any object that meets the protocol can be used with that software
Ruby Classes are Open for Extension
- New methods can be added to existing Ruby classes.
OO isn't a silver bullet solution to all softare problems, but it does
provide significant aid in developing programs.
| 1: def copy(source)
2: source.each { |item|
3: puts item
4: }
5: end
6: # Copy works with anthing that
7: # supports the `each' method.
8: # Such as ...
9: copy($stdin) # ... Files
10: copy(['one', 'two']) # ... Arrays
11: copy("one\ntwo\n") # ... Strings
|
1: class Integer
2: def twice
3: self + self
4: end
5: end
6:
7: puts 10.twice # => 20
|
|