Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Ruby Basic(3)
Author: Jason
Content
Class
Inheritance
Module
Mixin
Class Features
All instance variables are private by default
No need to declare instance variables
Class is mutable
Method...
Class is mutable
Change the method length of String
class String
def length
0
end
end
a = "I am a very long string"
a.leng...
Method though message
1.next # => 2
1.send(:next) # => 2
Class Example
class Book
def initialize(isbn, price)
@isbn = isbn
@price = price
end
end
book = Book.new("a book",100)
par...
Getter
def isbn=(isbn)
@isbn = isbn
end
Setter
def isbn
isbn
end
Generate getter &
setter
class Book
attr_reader :isbn # => getter
attr_writer :isbn # => setter
attr_accessor :isbn # => g...
Access Control
protected
def xxx
end
private
def yyy
end
Inheritance
class Parent
def say_hello
"Hello "
end
end
class Child < Parent
end
c = Child.new
c.say_hello # => "Hello "
Module Feature
Group variables , classes, methods
Act as namespace (~sandbox)
Cannot have instance, but have instance
meth...
Module Example
module Trig
PI = 3.1415
def Trig.sin(x)
...
end
end
require 'trig'
y = Trig.sin(Trig::PI/4)
Mixin
Another way to increase functionality of the
class
Include the module in the class
Instance methods will be included...
Mixin Example
module Debug
def who_am_i?
"debug: #{self.to_s}"
end
end
Mixin Example(cont’d)
class Person
include Debug
def to_s
"I am Person"
end
end
p = Person.new
p.who_am_i? # => "debug: I ...
True power of mixin
Give you a lot of functionality
But you only have to implement one or two
methods
Great flexibility
Eg....
Comparable Example
class Person
include Comparable
attr_reader :name
def initialize(name)
@name = name
end
Comparable (cont’d)
def to_s
"#{@name}"
end
def <=>(other)
self.name <=> other.name
end
end
Only method to implement
Comparable (cont’d)
p1 = Person.new("Matz")
p2 = Person.new("Guido")
p3 = Person.new("Larry")
# Compare a couple of names
...
Comparable (cont’d)
# Sort an array of Person objects
puts "Sorted list:"
puts [p1, p2, p3].sort
# Guido
# Larry
# Matz
Instance variable in
Mixin
There can be instance variables in Mixin,
Though the getter and lazy instantiation.
Lazy Loading Example
module Observable
def observer
@observer_list ||= []
end
...
end
Crazy short cut
Crazy Syntax?
@observer_list ||= []
is equal to
@observer_list = @observer_list || []
If @observer_lists exist?,
assign it...
Upcoming SlideShare
Loading in …5
×

Ruby basic3

344 views

Published on

Introduce class, inheritance, module, mixing in Ruby

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Ruby basic3

  1. 1. Ruby Basic(3) Author: Jason
  2. 2. Content Class Inheritance Module Mixin
  3. 3. Class Features All instance variables are private by default No need to declare instance variables Class is mutable Method is though sending message Initialize is private method
  4. 4. Class is mutable Change the method length of String class String def length 0 end end a = "I am a very long string" a.length # => 0
  5. 5. Method though message 1.next # => 2 1.send(:next) # => 2
  6. 6. Class Example class Book def initialize(isbn, price) @isbn = isbn @price = price end end book = Book.new("a book",100) parameter from new @xx are instance variable
  7. 7. Getter def isbn=(isbn) @isbn = isbn end
  8. 8. Setter def isbn isbn end
  9. 9. Generate getter & setter class Book attr_reader :isbn # => getter attr_writer :isbn # => setter attr_accessor :isbn # => getter & setter ..... end
  10. 10. Access Control protected def xxx end private def yyy end
  11. 11. Inheritance class Parent def say_hello "Hello " end end class Child < Parent end c = Child.new c.say_hello # => "Hello "
  12. 12. Module Feature Group variables , classes, methods Act as namespace (~sandbox) Cannot have instance, but have instance method Can have module method, module.method Access content by ::
  13. 13. Module Example module Trig PI = 3.1415 def Trig.sin(x) ... end end require 'trig' y = Trig.sin(Trig::PI/4)
  14. 14. Mixin Another way to increase functionality of the class Include the module in the class Instance methods will be included in the class Like “mix” in the class
  15. 15. Mixin Example module Debug def who_am_i? "debug: #{self.to_s}" end end
  16. 16. Mixin Example(cont’d) class Person include Debug def to_s "I am Person" end end p = Person.new p.who_am_i? # => "debug: I am Person" include from Debug
  17. 17. True power of mixin Give you a lot of functionality But you only have to implement one or two methods Great flexibility Eg. Comparable, you have to implement <=>(other) in order to gain the ability to be compared
  18. 18. Comparable Example class Person include Comparable attr_reader :name def initialize(name) @name = name end
  19. 19. Comparable (cont’d) def to_s "#{@name}" end def <=>(other) self.name <=> other.name end end Only method to implement
  20. 20. Comparable (cont’d) p1 = Person.new("Matz") p2 = Person.new("Guido") p3 = Person.new("Larry") # Compare a couple of names if p1 > p2 puts "#{p1.name}'s name > #{p2.name}'s name" # => Matz's name > Guido's name end Thanks to Comparable
  21. 21. Comparable (cont’d) # Sort an array of Person objects puts "Sorted list:" puts [p1, p2, p3].sort # Guido # Larry # Matz
  22. 22. Instance variable in Mixin There can be instance variables in Mixin, Though the getter and lazy instantiation.
  23. 23. Lazy Loading Example module Observable def observer @observer_list ||= [] end ... end Crazy short cut
  24. 24. Crazy Syntax? @observer_list ||= [] is equal to @observer_list = @observer_list || [] If @observer_lists exist?, assign it to @observer_list else assign [] to @observer_list You may also consider this as either of them exist, assign it to the instance variable

×