ruby basic3

24
Ruby Basic(3) Author: Jason

Upload: ho-yin-liu

Post on 24-Jan-2018

312 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Ruby basic3

Ruby Basic(3)Author: Jason

Page 2: Ruby basic3

Content

Class

Inheritance

Module

Mixin

Page 3: Ruby basic3

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

Page 4: Ruby basic3

Class is mutableChange the method length of String

class String

def length

0

end

end

a = "I am a very long string"

a.length # => 0

Page 5: Ruby basic3

Method though message

1.next # => 2

1.send(:next) # => 2

Page 6: Ruby basic3

Class Exampleclass Book

def initialize(isbn, price)

@isbn = isbn

@price = price

end

end

book = Book.new("a book",100)

parameter from new

@xx are instance variable

Page 7: Ruby basic3

Getter

def isbn=(isbn)

@isbn = isbn

end

Page 8: Ruby basic3

Setter

def isbn

isbn

end

Page 9: Ruby basic3

Generate getter & setter

class Book

attr_reader :isbn # => getter

attr_writer :isbn # => setter

attr_accessor :isbn # => getter & setter

.....

end

Page 10: Ruby basic3

Access Control protected

def xxx

end

private

def yyy

end

Page 11: Ruby basic3

Inheritanceclass Parent

def say_hello

"Hello "

end

end

class Child < Parent

end

c = Child.new

c.say_hello # => "Hello "

Page 12: Ruby basic3

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 ::

Page 13: Ruby basic3

Module Examplemodule Trig

PI = 3.1415

def Trig.sin(x)

...

end

end

require 'trig'

y = Trig.sin(Trig::PI/4)

Page 14: Ruby basic3

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

Page 15: Ruby basic3

Mixin Example

module Debug

def who_am_i?

"debug: #{self.to_s}"

end

end

Page 16: Ruby basic3

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

Page 17: Ruby basic3

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

Page 18: Ruby basic3

Comparable Exampleclass Person

include Comparable

attr_reader :name

def initialize(name)

@name = name

end

Page 19: Ruby basic3

Comparable (cont’d)

def to_s

"#{@name}"

end

def <=>(other)

self.name <=> other.name

end

end

Only method to implement

Page 20: Ruby basic3

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

Page 21: Ruby basic3

Comparable (cont’d)

# Sort an array of Person objects

puts "Sorted list:"

puts [p1, p2, p3].sort

# Guido

# Larry

# Matz

Page 22: Ruby basic3

Instance variable in Mixin

There can be instance variables in Mixin,

Though the getter and lazy instantiation.

Page 23: Ruby basic3

Lazy Loading Example

module Observable

def observer

@observer_list ||= []

end

...

endCrazy short cut

Page 24: Ruby basic3

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